I've seen monitoring programs either in scripts that check process status using 'ps' or 'service status(on Linux)' periodically, or in C/C++ that forks and wait on the process...
I wonder if it is possible to use bash with trap and restart the sub-process when SIGCLD received?
I have tested a basic suite on RedHat Linux with following idea (and certainly it didn't work...)
#!/bin/bash
set -o monitor # can someone explain this? discussion on Internet say this is needed
trap startProcess SIGCHLD
startProcess() {
/path/to/another/bash/script.sh & # the one to restart
while [ 1 ]
do
sleep 60
done
}
startProcess
what the bash script being started just sleep for a few seconds and exit for now.
several issues observed:
- when the shell starts in foreground, SIGCHLD will be handled only once. does trap reset signal handling like signal()?
- the script and its child seem to be immune to SIGINT, which means they cannot be stopped by ^C
- since cannot be closed, I closed the terminal. The script seems to be HUP and many zombie children left.
- when run in background, the script caused terminal to die
... anyway, this does not work at all. I have to say I know too little about this topic. Can someone suggest or give some working examples? Are there scripts for such use?
how about use wait in bash, then?
Thanks
#!/bin/bash thepid=0 stopnow=0 set -o monitor trap cleanup SIGINT SIGTERM cleanup() { trap - SIGINT SIGTERM stopnow=1 if [ ${thepid} -ne 0 ] then echo killing ${thepid} kill ${thepid} fi } while [ ${stopnow} -ne 1 ] do echo starting ./trial.sh & thepid=$! echo "waiting on ${thepid}" wait ${thepid} done exit 0
– Joann