Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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/) :

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/ :

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


[ -x $exe ] || exit 0


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

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

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

case "$1" in
        status ${pname}
        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

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

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 – MᴀʀɪᴜsᴢS Apr 4 '14 at 18:38
This solved my issue today. In Ubuntu, I copied /etc/init.d/skeleton and couldn't figure out why it wasn't running in the background. I assumed it was already set up for background, but turns out it is not. – Ryan Nov 10 '15 at 2:40

Your Answer


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.