Problem solve Get help with specific problems with your technologies, process and projects.

How do you make Perl properly kill a subprocess?

How do you make perl properly kill a subprocess? I use:

if ($chiproc==0) { system("runsomething"); }
if ($chiproc!=0) { sleep(10); kill 9,$chiproc; }

This kills the forked process, but it does nothing to "runsomething" that it created.
You're detecting the cloned process correctly, but the system() call creates a third process, which I don't think you mean to do. What you want to do is replace the program in the cloned process (that's the second copy of the perl interpreter running your script) with your "runsomething" program.

To do that just use exec() instead of system(). It overwrites the current process with a new program. It's documented in the perlfunc(1) manual page, and in exec(2). fork() and exec() used as a pair in this way is a very common idiom.

Dig Deeper on Linux servers

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.