I'm trying to run a PHP background Process that is a consumer for Rabbit MQ messages. I can simply run the script as "php receive.php &", but I need to ensure that this script is always running.

I have put together a bash script that looks tries to find the PID of the process. If it doesn't find the PID, it starts the script back up. If it does find the process, it just echos the PID.

I am trying to run the Bash Script on a Cron, every hour, to ensure that the background process is running. But I'm it's not activating. If I run the Shell/Bash Script directly. It works perfectly.

Bash Script

p=$(pidof -s php receive.php)
if [[ -z "${p// }" ]]
        then php /var/www/html/example.com/public_html/receive.php &
        else echo 'Receive Already Running';

Crontab -e line

# m h  dom mon dow   command
0 * * * * /var/www/html/example.com/public_html/analytics.sh

What can I do different, to make sure that this background process is always running, and if its not, have it restart itself?

  • what init system does this linux host use? (e.g. systemd, sysv, upstart, ...) – thrig May 4 '18 at 14:00
  • Why use cron? Surely the answer is to launch from an enclosing script with while true; do php receive.php; done. This will restart immediately without waiting for the next cron poll. – AFH May 4 '18 at 14:06
  • We are using Ubuntu 16.04 LTS with PHP 7. We are using systemd. – user1732920 May 4 '18 at 14:09
  • Hey AFH, I understand that I can simply run the receive.php as a background process, but I need to ensure that it is always running. Even after an apache restart, or a Linux reboot. – user1732920 May 4 '18 at 14:17

You can create a systemd service that runs your bash script, but the script should be able to continuously run.

Create a file at /etc/systemd/system/

Description=Add a description here



run systemctl daemon-reload, and then systemctl start SERVICENAME


if the process you want to monitor already is a systemd service, modify the service to have Restart=always.


If the service is running as intended you will need to enable it to run on boot: systemctl enable SERVICENAME

  • This approach works if I start it manually using sudo systemctl start analytics.service But it doesn't start up on system boot. I tried sudo systemctl enable analytics.service, but it doesn't fire back up on system boot. Any ideas? – user1732920 May 4 '18 at 15:20
  • Can you show me the output of systemctl status analytics.service? Also, who is the owner of the file? It could be you need to add User=Add user name here under [Service]. – rowan May 4 '18 at 15:22
  • â analytics.service - Analytics Receiver Loaded: loaded (/etc/systemd/system/analytics.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2018-05-04 11:32:49 EDT; 43s ago Main PID: 4075 (php) CGroup: /system.slice/analytics.service ââ4075 /usr/bin/php /var/www/html/example.com/public_html/receive.php & – user1732920 May 4 '18 at 15:34
  • I changed from running a bash script, to running the php file directly in the background. The bash script and php script is owned by my sudo user. vupmedia – user1732920 May 4 '18 at 15:36
  • Loaded: loaded (/etc/systemd/system/analytics.service; enabled; vendor preset: enabled) Active: inactive (dead) (Result: exit-code) since Fri 2018-05-04 11:46:57 EDT; 39s ago Process: 3522 ExecStart=/usr/bin/php /var/www/html/analytics.vupinteractive.com/public_html/receive.php & (code=exited, status=255) Main PID: 3522 (code=exited, status=255) – user1732920 May 4 '18 at 15:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.