in reply to Is it possible to background a perl script from within itself?

I typically use a variation of this 'daemonize' code taken from perlipc. This sets up the script's environment and disassociates it from the caller entirely.
sub daemonize { chdir '/' or die "Can't chdir to /: $!"; open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, '>/dev/null' or die "Can't write to /dev/nul +l: $!"; defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; setsid or die "Can't start a new session: $!"; open STDERR, '>&STDOUT' or die "Can't dup stdout: $!"; }
You could also open STDOUT to point to, say, a log file here. Or a tied filehandle passing its data to syslog.
  • Comment on Re: Is it possible to background a perl script from within itself?
  • Download Code