title: Running a PHP Script as a Daemon in Debian
body: <p>I am trying to start a php script as a daemon in Debian. I also would like it to start on boot as well. </p>
  
  <p>I have been starting using <code>/path/to/php /path/to/script/Insert.php &amp;</code> without issue, and can <code>shell_exec("nohup /path/to/php /path/to/script/Insert.php &gt;/dev/null &amp;")</code> as well. I have tried using the below script, but it does not take the script into an operational state.</p>
  
  <p>Am copying the file to <code>/etc/init.d/</code> and using <code>update-rc.d</code> without issues. I can use <code>service congen-insert start</code>to 'start' the script, but it doesn't seem to actually run, and it doesn't start doing any work.</p>
  
  <p>What am I missing, or where have I gone wrong with the scripts?</p>
  
  <p>I know there are several ways to work around this, but I am really just trying to understand what I am doing incorrectly or why what I am doing is not working. </p>
  
  <p>Any help or suggestions is extremely appreciated! If there is anything else you need or anything I have missed in my description, please let me know so I can correct it.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>Service script</strong></p>
  
  <pre><code>#! /bin/sh
  ### BEGIN INIT INFO
  # Provides:          congen-insert
  # Required-Start:    $local_fs $network
  # Required-Stop:     $local_fs
  # Default-Start:     2 3 4 5
  # Default-Stop:      0 1 6
  # Short-Description: congen-insert
  # Description:       DB Insert Daemon
  ### END INIT INFO
  
  NAME="congen-insert"
  DESC=" DB Insert Daemon"
  PIDFILE="/var/run/${NAME}.pid"
  LOGFILE="/var/log/${NAME}.log"
  
  DAEMON="/path/to/php"
  DAEMON_OPTS="/path/to/script/Insert.php"
  
  START_OPTS="--start --background --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON} ${DAEMON_OPTS}"
  STOP_OPTS="--stop --pidfile ${PIDFILE}"
  
  test -x $DAEMON || exit 0
  
  set -e
  
  case "$1" in
      start)
          echo -n "Starting ${DESC}: "
          start-stop-daemon $START_OPTS &gt;&gt; $LOGFILE
          echo "$NAME."
          ;;
      stop)
          echo -n "Stopping $DESC: "
          start-stop-daemon $STOP_OPTS
          echo "$NAME."
          rm -f $PIDFILE
          ;;
      restart|force-reload)
          echo -n "Restarting $DESC: "
          start-stop-daemon $STOP_OPTS
          sleep 1
          start-stop-daemon $START_OPTS &gt;&gt; $LOGFILE
          echo "$NAME."
          ;;
      status)
      echo -n "Sorry, this isn't implemented yet"
      ;;
      *)
          N=/etc/init.d/$NAME
          echo "Usage: $N {start|stop|restart|force-reload}" &gt;&amp;2
          exit 1
          ;;
  esac
  
  exit 0
  </code></pre>
  
  <p><strong>Script I am trying to run:</strong></p>
  
  <pre><code>const LoaderPath = __DIR__ . DIRECTORY_SEPARATOR . ".." .DIRECTORY_SEPARATOR . "includes.php";
  
  require_once  LoaderPath;
  
  use PhpAmqpLib\Channel\AMQPChannel;
  use PhpAmqpLib\Message\AMQPMessage;
  use requests\InsertRequest;
  
  $connection = GetRabbitConnection();
  
  $channel = $connection-&gt;channel();
  
  $RedisClient = GetRedisClient();
  
  DeclareQueues($connection, $RedisClient);
  
  $MySQLHost = $RedisClient-&gt;get(MySQLHostKey);
  $MySQLUser = $RedisClient-&gt;get(MySQLUserKey);
  $MySQLPassword = $RedisClient-&gt;get(MySQLPasswordKey);
  $MySQLDatabase = $RedisClient-&gt;get(MySQLDatabaseKey);
  
  $InsertExchange = $RedisClient-&gt;get(Insert.":".Exchange);
  $InsertQueue = $RedisClient-&gt;get(Insert.":".Queue);
  $Prefetch = $RedisClient-&gt;get(Insert.":".Prefetch);
  
  $RedisClient-&gt;disconnect();
  $RedisClient = null;
  
  $mysql= new mysqli($MySQLHost, $MySQLUser, $MySQLPassword, $MySQLDatabase);
  
  $channel-&gt;basic_qos(0,$Prefetch,false);
  
  $channel-&gt;basic_consume($InsertQueue, $InsertExchange, false, false, false, false, "callback");
  
  
  echo "Consuming on Exchange $InsertExchange with Queue $InsertQueue\n";
  
  while(true) {
      $channel-&gt;wait();
  }
  
  $channel-&gt;close();
  
  function callback(AMQPMessage $message){
      global $mysql;
      echo "Message received", "\n";
      $InsertRequest = new InsertRequest($message-&gt;body);
  
      echo "Running Insert Statement\n";
      if (!$mysql-&gt;query($InsertRequest-&gt;SQL)){
          echo "Error: ".$mysql-&gt;error;
      }
  
      /** @type AMQPChannel $channel */
      $channel = $message-&gt;delivery_info['channel'];
      $channel-&gt;basic_ack($message-&gt;delivery_info['delivery_tag']);
      echo "Insert Complete\n";
  
  }
  </code></pre>