title: How can I check if a program is running in the background using a cron, and start it if needed?
body: <p>I have the task to run a daemon in the background on a production server. However, I do want to be sure that this daemon always runs. The daemon is PHP process.</p>
  
  <p>I tried to approach this by checking if the daemon is running, and if not: start it. So I have a command like:</p>
  
  <pre><code>if [ $(ps ax | grep -c "akeneo:batch:job-queue-consumer-daemon") -lt 3 ]; then php /home/sibo/www/bin/console akeneo:batch:job-queue-consumer-daemon &amp; fi
  </code></pre>
  
  <p>I first do an <code>if</code> with <code>ps</code> and <code>grep -c</code> to check if there are processes running with a given name, and if not: I start the command ending with an <code>&amp;</code>, forcing it to start.</p>
  
  <p>The above command works, if I execute it from the command line the process gets started and I can see that is is running when I execute a simple <code>ps ax</code>-command.</p>
  
  <p>However, as soon as I try to do this using the crontab it doesn't get started:</p>
  
  <pre><code>* * * * * if [ $(ps ax | grep -c "akeneo:batch:job-queue-consumer-daemon") -lt 3 ]; then php /home/sibo/www/bin/console akeneo:batch:job-queue-consumer-daemon &amp; fi
  </code></pre>
  
  <p>I also set the <code>MAILTO</code>-header in the crontab, but I'm not getting any e-mails as well.</p>
  
  <p>Can anyone tell me what's wrong with my approach? And how I can get it started?</p>