in reply to Run Perl script in perl program with new process group

...which is not working as expected.

So what do you expect?  Basically, the approach is fine — except that you got it the wrong way round wrt what is child and parent (if ($pid == 0) {...} is the child).

Here's a slightly modified version which does what I would expect, i.e. have perl.pl (and its children, if any) run under a new process group:

#!/usr/bin/perl use strict; use warnings; my $cmd = qq{nohup ./perl.pl &}; my $pid = fork; die 'fork failed' unless defined $pid; if ($pid) { print STDERR "parent pid=$$ child pid=$pid\n"; wait; my $sid = getppid; system "ps f -s $sid -o pid,ppid,pgrp,sid,cmd"; } else { print STDERR "child pid=$$ \$pid=$pid\n"; setpgrp; exec $cmd or die "Bad exec: $!"; }

With perl.pl being

#!/usr/bin/perl system 'sleep 1';

the output is something like

$ ./813283.pl child pid=31851 $pid=0 parent pid=31850 child pid=31851 PID PPID PGRP SID CMD 18307 18305 18307 18307 bash -rcfile .bashrc 31850 18307 31850 18307 \_ /usr/bin/perl ./813283.pl 31853 31850 31850 18307 \_ ps f -s 18307 -o pid,ppid,pgrp,sid,cmd 31852 1 31851 18307 /usr/bin/perl ./perl.pl 31854 31852 31851 18307 \_ sleep 1

As you can see in the PGRP column, perl.pl and its child process sleep 1 is running under a different process group (31851 in the sample output).

Replies are listed 'Best First'.
Re^2: Run Perl script in perl program with new process group
by veshwar (Initiate) on Dec 18, 2009 at 12:06 UTC
    in the output we are getting PGRP different from PID 31852 1 31851 18307 /usr/bin/perl ./perl.pl 31854 31852 31851 18307 \_ sleep 1 31852 is different from 31851... i am trying to start the perl.pl in background with PID = PGRP.. if i start the job without Background.. it works as expected but when i start the job on Background(&).

      If you start a job in the foreground (my $cmd = qq{nohup ./perl.pl};), then the process id (PID) and process group (PGRP) will match, because perl will exec the program directly.

      If you start a job in the background (my $cmd = qq{nohup ./perl.pl &};), then the process id (PID) and process group (PGRP) will NOT match, because perl notices the shell meta-character '&' and execs a shell directly, and the shell will exec the program. The PGRP you are seeing is the PID of the shell that was started solely to process the backgrounding character.

      Since you're already forking, why do you need the implied fork of the '&' character too?

        i need to make the process (my $cmd = qq{nohup ./perl.pl};) a daemon process. if i remove the &, it remains connected with the shell and i cannot disconnect the script. I have tried to start my script in background and starting process without &. It works but the ppid(20753) is still connected to shell which is closing on the logout. $ ps -ef -o "user,pid,ppid,pgid,args" | grep perl.pl userid 22542 20753 22542 /usr/bin/perl ./perl.pl Is there anyway i can change the ppid of this process to 1 without loggingout for the session.