http://qs321.pair.com?node_id=1160779


in reply to How do you launch a background process and get pid?

Just to clarify what AM said above: because you are using both redirection and backgrounding in the string passed to "exec", a shell process has to be launched to execute that command line, and the shell exits. If you don't background it, the shell will keep running for as long as the sleep, and your "ps ax | grep sleep" step will show three lines of output - the additional line would be the shell process that is running "sleep 30".

Apart from that, you should be aware that the "exit" statement in your "if" block is never reached -- as stated in the perlfunc manpage, the "exec" call never returns.

If you remove both backgrounding and redirection from the command line string, your parent perl process will get the pid for the child sleep process, because "exec" won't have to invoke a shell to run the command -- i.e. you could do either this:

exec( "sleep 30" );
or this:
exec( "sleep", "30" );