|
Recently did something similar with trap:
#!/bin/bash
# /usr/bin/process_1
MY_PID=$$
trap_int() {
sleep .5
process_2 &> /dev/null
}
while [ -d /proc/$MY_PID ] ; do
trap trap_int 2
# note that leaving out this initial start of the program means that the loop waits for
# an INT signal before starting the program above.
process_2 &> /dev/null
This code is meant to keep re-starting process_2 whenever it gets stopped or interrupted (kill-2 process_2.).
In order to kill both processes you can send kill to process_1 first then to process_2. Play around with this code by sending kill 2, 5 and 9 to both process in both orders, then commenting out the last line and repeating. Some very interesting behaviour is available...
My program uses the above to create a non-polling wrapper process which keeps another program up-to-date by allowing other programs to interrupt it(process_2) process_1 keeps restarting it. process_1 acts very similar to a daemon which keeps process_2 running, but without constantly using CPU cycles(sleeps) to poll the process -kind of a one-way pipe. The third programs which 'communicate' with process_2 do so by rewriting its' config file. When process_1 restarts process_2 the config file gets re-read.
This setup will give you a stable process id for process_1, but process_2 will have a new pid each time it gets restarted. I use pgrep to help sort out the process id's.
I think what you want is slightly different, but playing around with this you can make it behave other ways. As mentioned, commenting out the last line causes process_2 to be started if you send kill -2 to process_1.
Note that the sleeps in my code are not needed, but make the response of the program smoother. These are not getting looped over and over.
Last edited by gnashley; 06-15-2007 at 04:02 AM.
|