title: Call to daemon in a /etc/init.d script is blocking, not running in background
body: <p>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 <code>/usr/sbin/</code>) :</p>
  
  <pre><code>#!/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;
  }
  </code></pre>
  
  <p>So this script basically prints something every 3 seconds.</p>
  
  <p>Then, as I want to daemonize this script, I've also put this bash script (also called synpipe_server) in <code>/etc/init.d/</code> :</p>
  
  <pre><code>#!/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 ] &amp;&amp; touch ${lockfile}
      echo $PID &gt; ${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
  </code></pre>
  
  <p>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 <code>/dev/null</code> if I execute :</p>
  
  <pre><code>service synpipe_server start
  </code></pre>
  
  <p>But here is what I get instead :</p>
  
  <pre><code>[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]# 
  </code></pre>
  
  <p>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 <em>daemon</em>).</p>
  
  <p>Does anyone have any idea of what I am doing wrong ?</p>
  
  <p>EDIT : maybe I should say that I am on a Red Hat machine.</p>
  
  <pre><code>Scientific Linux SL release 5.4 (Boron)
  </code></pre>
  
  <p>Thanks,
  Tony</p>