Skip to content

Instantly share code, notes, and snippets.

@krimdomu
Last active August 29, 2015 14:00
Show Gist options
  • Save krimdomu/2b57df6a894452ada759 to your computer and use it in GitHub Desktop.
Save krimdomu/2b57df6a894452ada759 to your computer and use it in GitHub Desktop.
daemonize a script/programm
#!/usr/bin/perl
# USAGE: daemonize --user root --group root --pid_file /var/run/mydaemon.pid --command /usr/local/bin/myscript
use strict;
use warnings;
use Net::Server::Daemonize 'daemonize';
use Getopt::Long;
use File::Basename;
my ( $user, $group, $pid_file, $command, $bind_to );
GetOptions(
"user=s" => \$user,
"group=s" => \$group,
"pid_file=s" => \$pid_file,
"command=s" => \$command
);
if ( !$user ) {
print "No user given.\n";
exit 1;
}
if ( !$group ) {
print "No group given.\n";
exit 1;
}
if ( !$pid_file ) {
print "No pid_file given.\n";
exit 1;
}
if ( !$command ) {
print "No command given.\n";
exit 1;
}
daemonize( $user, $group, $pid_file );
exec $command;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment