Skip to content

Instantly share code, notes, and snippets.

@trapd00r
Created December 17, 2011 20:36
Show Gist options
  • Select an option

  • Save trapd00r/1491310 to your computer and use it in GitHub Desktop.

Select an option

Save trapd00r/1491310 to your computer and use it in GitHub Desktop.
sub daemonize {
my $daemon_log = shift // '/dev/null';
use POSIX 'setsid';
my $PID = fork();
exit if $PID;
exit 1 if(!defined($PID));
setsid();
$PID = fork();
exit 1 if(!defined($PID));
if($PID) {
waitpid($PID, 0);
exit 0;
}
elsif($PID == 0) {
open(STDOUT, '>', $daemon_log);
open(STDERR, '>', '/dev/null');
open(STDIN, '<', '/dev/null');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment