Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a Perl script that I want to daemonize. Basically this perl script will read a directory every 30 seconds, read the files that it finds and then process the data. To keep it simple here consider the following Perl script (called synpipe_server, there is a symbolic link of this script in /usr/sbin/) :

#!/usr/bin/perl
use strict;
use warnings;

my $continue = 1;
$SIG{'TERM'}  = sub { $continue = 0; print "Caught TERM signal\n"; };
$SIG{'INT'} = sub { $continue = 0; print "Caught INT signal\n"; };

my $i = 0;
while ($continue) {
     #do stuff
     print "Hello, I am running " . ++$i . "\n";
     sleep 3;
}

So this script basically prints something every 3 seconds.

Then, as I want to daemonize this script, I've also put this bash script (also called synpipe_server) in /etc/init.d/ :

#!/bin/bash
# synpipe_server : This starts and stops synpipe_server
#
# chkconfig: 12345 12 88
# description: Monitors all production pipelines
# processname: synpipe_server
# pidfile: /var/run/synpipe_server.pid
# Source function library.
. /etc/rc.d/init.d/functions

pname="synpipe_server"
exe="/usr/sbin/synpipe_server"
pidfile="/var/run/${pname}.pid"
lockfile="/var/lock/subsys/${pname}"

[ -x $exe ] || exit 0

RETVAL=0

start() {
    echo -n "Starting $pname : "
    daemon ${exe}
    RETVAL=$?
    PID=$!
    echo
    [ $RETVAL -eq 0 ] && touch ${lockfile}
    echo $PID > ${pidfile}
}

stop() {
    echo -n "Shutting down $pname : "
    killproc ${exe}
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ]; then
        rm -f ${lockfile}
        rm -f ${pidfile}
    fi
}

restart() {
    echo -n "Restarting $pname : "
    stop
    sleep 2
    start
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    status)
        status ${pname}
    ;;
    restart)
        restart
    ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
    ;; esac

exit 0

So, (if I have well understood the doc for daemon) the Perl script should run in the background and the output should be redirected to /dev/null if I execute :

service synpipe_server start

But here is what I get instead :

[root@master init.d]# service synpipe_server start
Starting synpipe_server : Hello, I am running 1
Hello, I am running 2
Hello, I am running 3
Hello, I am running 4
Caught INT signal
                                                           [  OK  ]
[root@master init.d]# 

So it starts the Perl script but runs it without detaching it from the current terminal session, and I can see the output printed in my console ... which is not really what I was expecting. Moreover, the PID file is empty (or with a line feed only, no pid returned by daemon).

Does anyone have any idea of what I am doing wrong ?

EDIT : maybe I should say that I am on a Red Hat machine.

Scientific Linux SL release 5.4 (Boron)

Would it do the job if instead of using the daemon function, I use something like :

nohup ${exe} >/dev/null 2>&1 &

in the init script ?

share|improve this question
add comment

2 Answers

I suggest you daemonize the perl script directly instead of adding the extra layer of the redhat init script daemon function. It's hard to get daemons right if you try to write them on your own. Proc::Daemon is pretty straightforward.

Also, here's a discussion of how to write perl daemons.

Bonus answer: use daemontools and Proc::Daemontools. That provides a comprehensive daemon management system and you probably already have daemontools installed anyway. Some people dislike daemontools but it gets the job done.

No matter how many time I write daemon is still seems weird. Maybe I should just use dæmon.

share|improve this answer
add comment

If you are using Debian and its derivatives, use start-stop-daemon with -b option to start your process without problem.

share|improve this answer
    
This is RedHat machine, so should use daemon and killproc instead –  MariuszS Apr 4 at 18:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.