Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Perl daemons and /etc/init.d scripts

by bikeNomad (Priest)
on Aug 17, 2001 at 18:11 UTC ( [id://105674] : perlquestion . print w/replies, xml ) Need Help??

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

I've written a couple of daemons in Perl that I want to be controlled by the SysV init subsystem in Debian GNU/Linux (2.2.19 kernel). They provide a start-stop-daemon utility that will stop and start other programs; it knows enough to check the running process information against the PID saved in a file in /var/run.

The problem is that when I go to stop my Perl daemon, the start-stop-daemon can't find the process. It looks at the PID and determines that the name of the running process is not the same as the script name (of course, the running process is named "perl").

I've tried assigning to $0 to fake the name, but this doesn't work either (the name does not compare the same). It also seems that assigning to $0 does something funny to the apparent process name; this program:

#!/usr/bin/perl -w my $pid = fork; exit if $pid; $0 = "a"; sleep 60;

causes ps to show the name of the process as "a" followed by over 900 spaces.

I have also tried changing the --name and --exec options to start-stop-daemon to various flavors of perl, /usr/bin/perl, "/usr/bin/perl", all without success.

Has anyone got an idea of how to get this working? The reason I wanted to use start-stop-daemon is that it looks at the process information to determine whether the PID in the file is in fact valid.

Replies are listed 'Best First'.
Re: Perl daemons and /etc/init.d scripts
by bikeNomad (Priest) on Aug 17, 2001 at 18:21 UTC
    OK, this seems to work, setting --exec to the name of Perl, and --startas to the name of the script (one last try did it, apparently):

    #! /bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/home/wander/wander/LCD/ NAME=wanderLCD DESC="Wander LCD driver" test -f $DAEMON || exit 0 set -e case "$1" in start) echo -n "Starting $DESC: " start-stop-daemon --start --quiet --pidfile /var/run/$ \ --exec /usr/bin/perl --startas $DAEMON echo "$NAME." ;; stop) echo -n "Stopping $DESC: " # --quiet start-stop-daemon --stop --signal 15 --pidfile /var/run/$ \ --exec /usr/bin/perl --startas $DAEMON echo "$NAME." ;; restart|force-reload) echo -n "Restarting $DESC: " start-stop-daemon --stop --quiet --pidfile \ /var/run/$ --exec /usr/bin/perl --startas $DAEMON sleep 1 start-stop-daemon --start --quiet --pidfile \ /var/run/$ --exec /usr/bin/perl --startas $DAEMON echo "$NAME." ;; *) N=/etc/init.d/$NAME # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2 echo "Usage: $N {start|stop|restart|force-reload}" >&2 exit 1 ;; esac exit 0

    In the Perl script, I have something like:

    BEGIN { # Fork. my $pidFile = '/var/run/'; my $pid = fork; if ($pid) # parent: save PID { open PIDFILE, ">$pidFile" or die "can't open $pidFile: $!\n"; print PIDFILE $pid; close PIDFILE; exit 0; } }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://105674]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-02-25 07:49 GMT
Voting Booth?
My favourite way to spend a leap day ...

Results (23 votes). Check out past polls.