php - How can I check if a program is running in the background using a cron, and start it if needed?

38

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.

I tried to approach this by checking if the daemon is running, and if not: start it. So I have a command like:

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 & fi

I first do anif withps andgrep -c to check if there are processes running with a given name, and if not: I start the command ending with an&, forcing it to start.

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 simpleps ax-command.

However, as soon as I try to do this using the crontab it doesn't get started:

* * * * * 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 & fi

I also set theMAILTO-header in the crontab, but I'm not getting any e-mails as well.

Can anyone tell me what's wrong with my approach? And how I can get it started?

33

Answer

Solution:

An easy and old-style one is to create a bash file where you basically check if the process is running, otherwise you start it.

Here the content of the bash file:

#!/bin/bash

if [ $(ps -efa | grep -v grep | grep job-queue-consumer-daemon -c) -gt 0 ] ;
then
    echo "Process running ...";
else
    php /home/sibo/www/bin/console akeneo:batch:job-queue-consumer-daemon
fi;

Then in the crontab file you run the bash file.

34

Answer

Solution:

There are special services for such tasks. For example http://supervisord.org/

Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.

And you can manage it via f.e https://github.com/supervisorphp/supervisor

484

Answer

Solution:

A command working on command line and not working in CRON, this happened to me and here is what solved my problem.

Runecho $PATH in your terminal, copy entire output.

Then typecrontab -e and at top of file, write this

PATH=WHATEVER_YOU_COPIED_FROM_LAST_COMMAND_OUTPUT

PS: (more suggestions)

I think you need to installapt-get install postfix on Ubuntu to be able to send emails.

You should also see CRON logs by

grep CRON /var/log/syslog
249

Answer

Solution:

i would recommend you to use supervisord, it handles these kinds of issues with automatic restart on failed services, additionaly, you can try to set the akeneo commands as a service.

Otherwise, if you would like to do it using cronjobs, you may have an issue with the php binary, you need to setup the absolute path :

e.g : /usr/bin/php

I would also recommend if you use cronjob:

  1. Check the logs of the cronjob for additional issues

    grep CRON /var/log/syslog

  2. Clean it up using a standalone bash script (don't forget to chmod +x)

People are also looking for solutions to the problem: FTP file upload in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.