H! I have written script where it need to invoke the perl script in background, then write the pid in temp file then bring back the job to foreground. whenever the Ctrl-C or Ctrl-Z is pressed in the script has to exit and prompt should be dispalyed. but this script causing exit from shell session itself if press Ctrl-C and I tried even after setting stty intr=^c also stty -a is fine. I want to trap both Ctrl-C or Ctrl-Z and it should not exit the session but only script should exit, not login shell.
Did you find any code problem? or what might be the problem? also below fg statement echoing the commond how can we avoid that. I used 2>/dev/null, till it not working.
Please help me in this.
I have written the code as below:
#!/bin/sh
cleanup()
{
echo "Caught Signal ... Terminated."
exit 1
}
...
while [ 1 ]
do
# enter the optional parameter if we need to
set -m
#invoke perl script in background
${HOME}/${PROCESS}.pl ${OPTIONAL} 2>&1 > $LOGFILENAME &
#check exit status
status=$?
if [ $status -ne 0 ]
then
echo ${PROCESS}.pl ${OPTIONAL} Job fails with status $status
exit $status
fi
# write the PID into files
echo $! > ${PID_DIR}/${PROCESS}.perl
chmod 755 ${PID_DIR}/${PROCESS}.perl
# then bring the job to foreground
fg %+ 2>/dev/null
trap 'cleanup' 2 9 15 17 19 20
sleep ${SLEEP};
done
exit 0
The value of $status is not the exit status of the Perl program, it is the exit status of "&" which would normally be zero. This affects the whole logic of your script.
Backgrounding the Perl program seems pointless because it is immediately brought to foreground.
To be effective the trap needs to be before the "while" line.
The script as it stands will try to start an infinite number of identical Perl programs unless the trap is triggered when it will execute "cleanup" and leave the Perl program(s) running. If the value of $SLEEP} is greater than the run time of one Perl program it may only run one Perl program at a time.
There is no provision for stopping the Perl program or waiting for it to finish before starting another one. It is unclear whether you want to stop the Perl program when someone presses ctrl/c or ctrl/z . Maybe I'm being thick but I don't understand what you are trying to do.
Actually this script was already written long back in sunsolaris and I need to migrate it to GNU linux. the exact code is as below
code:
while [ 1 ]
do
# enter the optional parameter if we need to
set -m
${HOME}/${PROCESS}.pl ${OPTIONAL} 2>&1 > $LOGFILENAME &
#check exit status
status=$?
if [ $status -ne 0 ]
then
echo ${PROCESS}.pl ${OPTIONAL} Job fails with status $status
exit $status
fi
echo $! > ${PID_DIR}/${PROCESS}.perl
chmod 755 ${PID_DIR}/${PROCESS}.perl
cat ${PID_DIR}/${PROCESS}.perl | ${MAIL_BIN} -s "\"${MAIL_HEADING1}\"" ${MAIL_LIST}
fg %+
cat ${PID_DIR}/${PROCESS}.perl | ${MAIL_BIN} -s "\"${MAIL_HEADING2}\"" ${MAIL_LIST}
sleep ${SLEEP};
done
exit 0
1) the main aim of running the perl script in background, is to send PERL script PID to mail when it started.
2) Once the script execution is competed then again script need to notify the finish.
3) here the SLEEP is set to 2
4) When I stared the script its