#!/usr/bin/perl use warnings; use strict; use Getopt::Long; use Proc::Daemon; use Cwd; use File::Spec::Functions; my $daemon = Proc::Daemon->new( work_dir => getcwd() ); my $pid; my $daemonize = 1; GetOptions( 'daemon!' => \$daemonize, "start" => \&run, "status" => \&status, "stop" => \&stop ); sub stop{ if($pid){ print "Stopping pid $pid... \n"; if($daemon->Kill_Daemon($pid)){ print "Successfully stopped. \n"; }else{ print "Could not find $pid. Was it running? \n"; } }else{ print "not Running, nothing to stop. \n"; } } sub status{ if($pid){ print "Running with pid $pid. \n"; }else{ print "Not running. \n"; } } sub run{ if (!$pid){ print "Starting...\n"; if($daemonize){ $pid = $daemon->Init; } while(1){ open(my $FH, '>>', catfile(getcwd(), "log.txt")); print $FH "Logging from PID $pid at " . time() . "\n"; close $FH; sleep 5; } }else{ print "No active daemons. \n"; } }