You should be able to double fork(), then dissociate the new child from the process group, then exec the other program from that grandchild.
POSIX::setsid() is your friend. POSIX is a core module, too, so its use is not subject to installation issues. Use it like this:
#!/usr/bin/perl -w
use strict;
use POSIX qw{setsid};
### or just 'use POSIX;' if you plan to use other
### functions from it, of course
if ( fork ) {
{
### grandparent code goes here
print "My grandkid should do a cute l'il ls...\n";
}
exit; ### die if we're the parent
}
if ( fork ) {
exit; ### die if we're the parent of this one, too.
}
### grandchild code starts here
POSIX::setsid; ### should be part of a new session now
exec '/bin/ls', '.';
The above program should show a proud grandparent
bragging about what its grandkid is doing. The grandchild then sticks around after the grandparent is dead. Use a loop in the grandchild if you need to prove it to yourself. The grandparent can also do whatever you want it to do, too.
Update:By all means, also close the standard file handles and chdir to the root directory as
edebill suggests. I was overlooking this as assumed for serious background tasks, but I should learn sooner or later that my assumptions are not necessarily those of another monk reading my nodes.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.