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.


In reply to Spawn off programs with qx and nohup by miketosh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.