sktemkar has asked for the wisdom of the Perl Monks concerning the following question:

hi, I have written a daemon example. But i dont know why it is not running. When i debugged using print statment, it is exiting after the (open STDIN, '/dev/null' or die "Can't read /dev/null: $!";) statement in the daemonize method. Can you please help me
#!/usr/bin/perl # daemonize the program &daemonize; $| = 1; while(1) { print "\nIn the While\n"; open(MYFILE,'>>temp.txt'); 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: $!"; umask 0; }

Edit: Code tags added by Corion

Replies are listed 'Best First'.
Re: Perl daemon exiting without warning
by cdarke (Prior) on Feb 11, 2011 at 09:43 UTC
    It would have helped if you used <code> ... </code> tags:
    #!/usr/bin/perl # daemonize the program print "Parent pid: $$\n"; # I added this &daemonize; $| = 1; while(1) { print "\nIn the While\n"; open(MYFILE,'>>temp.txt'); 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: $!"; umask 0; }
    Works for me:
    /home/user1> ./gashd.pl Parent pid: 7300 /home/user1> ps PID TTY TIME CMD 7190 pts/2 00:00:00 bash 7301 pts/2 00:00:02 gashd.pl 7315 pts/2 00:00:00 ps
    Do you have any error messages?

    Update: I was wrong, it is not working. My mistake was in copying your code, which does not have:
    use warnings; use strict;
    which points out that setsid is a bare-word. What you need is POSIX::setsid
    In fact the ps output should have shown me that it was not creating a new session. Here is a version, which also logs the error when trying to open the file.
    #!/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; }
    Check /var/log/messages (by default) for syslog messages, under an ordinary user I get:
    Feb 11 11:08:44 yogi ./gashd.pl[10467]: Unable to open temp.txt: Permi +ssion denied
Re: Perl daemon exiting without warning
by ikegami (Patriarch) on Feb 11, 2011 at 05:47 UTC

    I think your analysis of the problem is incorrect. I think successfully daemonised a program that repeatedly tries to write to /temp.txt and fails for permission reasons.

      There is permission for writing the file temp.txt. i debugged the daemon, it is exiting after the " open STDIN, '/dev/null' " statement.

        If you're saying it's exiting immediately after that open, I don't believe you. Did you perhaps trace the code by printing to STDOUT?

        I find it very unlikely that you have permission to create and write to /temp.txt (and that you intended to do so). Are you running as root?