#!/usr/bin/perl # daemonize the program use warnings; use strict; use POSIX ('setsid'); use Sys::Syslog qw(:standard); print "Parent pid: $$\n"; # I added this &daemonize; $| = 1; while(1) { print "\nIn the While\n"; if (!open(MYFILE,'>>temp.txt')) { my $err = $!; # In case Sys::Syslog resets it # Log pid with messages, set our facility to be LOCAL0 openlog($0, 'pid', 'LOG_LOCAL0'); syslog('LOG_ERR', "Unable to open temp.txt: $err"); closelog(); } print MYFILE "Hello Sri\n"; close(MYFILE); } 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/null: $!"; open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!"; defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; #setsid() or die "Can't start a new session: $!"; POSIX::setsid(); umask 0; }