http://www.perlmonks.org?node_id=1066375


in reply to is it possible to get PID for a command running background?

You're using system to launch a shell to execute a shell command. Perl has no way no knowing if that command results in the launching of processes or what their PID would be.

The simplest solution would be to launch the program yourself.

use IPC::Open3 qw( open3 ); sub launch_in_background { my ($prog, @args) = @_; open(local *CHILD_STDIN, '<', '/dev/null') or die $!; if (!@args) { # Make sure $prog isn't interpreted as a shell command. @args = ('-c', 'exec "$1"', 'dummy', $prog); $prog = '/bin/sh'; } return open3('<&CHILD_STDIN', '>&STDOUT', '>&STDERR', $prog,@args); } my $pid1 = launch_in_background('prog', 'file1'); my $pid2 = launch_in_background('prog', 'file2');

Don't forget to reap your children or set SIGCHLD to IGNORE when you launch them.

IPC::Run3 or IPC::Run would provide more readable solutions.