miketosh has asked for the wisdom of the Perl Monks concerning the following question:

Googling has led me to the following code, but it still does not act as expected. I've written a perl script to see if some foreign program is running (kicked off outside of my script) and if it is not, restart it (so that it will still run after the script has completed). I don't want my script sitting there waiting for the command to complete.

my $fullcmd="nohup sh -c \"($cmd)\" &"; debug(DWRN, "Process is not running, attempting to restart using ' +$fullcmd'\n"); qx/$fullcmd/; debug(DDBG, "Process is restarted, next poll in $interval seconds\ +n");

I'm using a sleep in my function call so that it is obvious what the relationship is between my perl script and the remote program. Here is the output when I run my command to monitor ps output for a sleep: (my debug function puts time and debug level at the start of the lines of text)

~$ perl bin/restart_app --cmd "sleep 5" -vvv "sleep" ...<snip>... 17:14:49 D1 Process is not running, attempting to restart using 'nohup + sh -c "(sleep 5)" &' 17:14:54 D2 Process is restarted, next poll in 2 seconds ...<snip>...

My BIGGEST concern is to make sure this executed program (sleep in this example) is not a child of the perl script. If someone else comes along and kills the xterm that the perl script is running in, the executed program should NOT close

UPDATE: (Answer: RTFM) Perlfaq says to use system("cmd &"). So changing the qx// to system(), but leaving the nohup alone appeared to do the trick. Here is the updated code snippet:

my $finish = time + $run; my $restarts=0; while($finish > (my $last = time)) { my $next = $last + $poll; unless(is_running()) { my $fullcmd="nohup sh -c \"($cmd)\" &"; system($fullcmd); }

If you ask "why do you have the command spawned off by sh, enclosed in parentheses?", I'd answer, because sometimes I may need to start two separate programs, and doing it this way gives me as much flexibility as typing in a shell.