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; }

In reply to Re: Run Perl script in perl program with new process group by gmargo
in thread Run Perl script in perl program with new process group by veshwar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.