Hi, I'm relatively new to perl programming, and I couldn't find an answer to this on perldoc (maybe I didn't know what to search for). I need to invoke a long-running shell script from a perl script, and the perl script needs to know the shell script's PID so that it can send it signals later. The shell script does a bunch of stuff and then sits in a sleep loop waiting for signals from the perl script.
I would like to execute the shell script as an independent process (i.e. fork then exec), and I don't want the perl script to wait for the shell script to exit before continuing, because it never will (until the perl script sends the right signal). As far as I can tell, the system() function doesn't do the right thing (waits for child to exit), neither does exec() (never returns), and I don't think qx/backticks would work either for the same reason as with system().
Assuming I can execute a shell script "in the background" and continue execution, I then need to get the PID of the child process. How do I do that? I know perl reserves $$ for the PID of the perl script and offers a getppid() function to get the parent PID, but how do I get the child PID?
In case I'm not being clear, this is what I want to do in shell syntax:
my_script.sh &
pid=$!
Of course, $! means something completely different in perl. I'm sure this is a not-so-uncommon programming task, so how do I do it?
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.