Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Run Perl script in perl program with new process group

by gmargo (Hermit)
on Dec 18, 2009 at 18:51 UTC ( [id://813416]=note: print w/replies, xml ) Need Help??


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

Here's some code to daemonize a program. With the test daemon I've provided, it is easy to confirm that the code runs even if you log off.

Here is the daemonizing code:

#!/usr/bin/perl -w use strict; use warnings; use diagnostics; use POSIX qw(setsid); my $cmd = qq{./perl_delayer.pl}; fork_daemon($cmd); exit 0; ################################################# # Daemonize! ################################################# sub fork_daemon { my ($cmd) = @_; # Pass a string or an array reference my $pid_c = fork(); # Parent spawns Child die "Cannot fork: $!" if !defined $pid_c; if ($pid_c) { # ==== Parent ==== waitpid($pid_c, 0); # Zombies not allowed return; # No attachment to grand-child } # ==== Child ==== my $pid_gc = fork(); # Child spawns Grand-Child die "Cannot fork: $!" if !defined $pid_gc; exit (0) if $pid_gc; # Child exits immediately # ==== Grand-Child ==== # Grand-Child continues, now parented by init. # Detach from controlling terminal and create new process group. setsid() || die "Can't start a new session: $!"; # Close std* streams. close STDIN; close STDOUT; close STDERR; open STDIN , "< /dev/null"; open STDOUT, "> /dev/null"; # could use "> nohup.out" open STDERR, "> /dev/null"; # could use ">> nohup.out" # There is no reason to ignore SIGHUP. # Since we've detached from the terminal, we won't # be getting one when the user logs out. # $SIG{HUP} = 'IGNORE'; exec(ref($cmd) eq "ARRAY" ? @$cmd : $cmd); # Run command exit(0); }

Here is the test daemon code, "perl_delayer.pl". You will observe that the file created ("delayer.out") gets the full countdown and is not affected by a logout.

#!/usr/bin/perl -w use strict; use warnings; my $fname = "delayer.out"; open(OUT,">",$fname) || die("Cannot open $fname: $!"); select OUT; $| = 1; # make unbuffered select STDOUT; $| = 1; # make unbuffered my $count = 60; while ($count--) { my $str = "$count: The time is now ".localtime()."\n"; print $str; print OUT $str; sleep 1; }

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 20:27 UTC
    Thanks for All who have helped me in here. Everything is working now as expected. Was using system() instead of exec and there was some confusion created due to lot of debug print statements. After removing all print statements it is working now as expected. $cmd = qq{nohup ./perl.pl}; my $pid = fork; if ($pid) { } else{ # Must do setpgrp as child for nohup and job control stuff... setpgrp || die "Cannot do SetGroup\n"; exec "$cmd" || die "Bad exec $!"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://813416]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-25 23:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found