1

My issue seems to be asked before but hold on, this one is a bit different.

I have 2 php files, I run the following commands:

nohup php file_1.php >/dev/null 2>&1 &
nohup php file_2.php >/dev/null 2>&1 &

This will create basically 2 php processes.

My problem is that sometimes one of the files or even both of them are killed by the server for unknown reason and I have to re-enter the commands over again. I tried 'forever' but doesn't help.

If the server is rebooted I will have to enter those 2 commands, I thought about Cronjob but I'm not sure if it would launch it twice which would create more confusion.

My question is how to start automatically the files if one or both of them got killed? What is the best way to achieve this which would check exactly that file_1.php or that file_2.php is indeed running?

Zack Tim
  • 66
  • 7
  • How does the scripts look? – Cyclonecode May 15 '21 at 23:11
  • they are actually infinite loops that search in a database. – Zack Tim May 15 '21 at 23:14
  • See this which has a bunch of options. We use supervisord where I work. https://stackoverflow.com/a/16577806/231316 – Chris Haas May 15 '21 at 23:18
  • Seems interesting, thank you @ChrisHaas Is it the best way to achieve it? in case of a reboot, should I dig in on how to start it automatically? – Zack Tim May 15 '21 at 23:21
  • 1
    Supervisord, by default (if I remember correctly) will auto load on restart. You could also look at systemd and upstart for real "services" if you want more common control patterns. Honestly, 99% of the time, running cron every minute is good enough, too, and use flock or similar to guarantee there’s no overlap. – Chris Haas May 15 '21 at 23:28
  • Windows or Linux Server? – Kevin M. Mansour May 15 '21 at 23:33
  • @KevinM.Mansour Ubuntu Server 18.04 – Zack Tim May 15 '21 at 23:39
  • dont need any fancy tools a simple while loop will work `@reboot while sleep 1; do cd /var/www/html && /usr/bin/php file_1.php ; done >/dev/null 2>&1` – Lawrence Cherone May 16 '21 at 00:29
  • I was thinking more of the reason why the script is actually terminating? Is it timing out or is it something else? As long as they are running as they should and is not unexpectedly terminated you should not even have this problem. – Cyclonecode May 16 '21 at 08:01

2 Answers2

4

There are a couple of ways you can do this. As @Chris Haas mentioned in the comments, supervisord can do this for you, or you can run a watchdog shell script from cron at regular intervals that checks to see if your script is running, and if not, start it. Here's one I use.

#!/bin/bash
FIND_PROC=`ps -ef |grep "php queue_agent.php --env prod" | awk '{if ($8 !~ /grep/) print $2}'`
# if FIND_PROC is empty, the process has died; restart it

if [ -z "${FIND_PROC}" ]; then
      DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
      echo queue_agent.php failed at `date`
      cd  ${DIR}
      nohup nice -n 10 php queue_agent.php --env prod -v > ../../sandbox/logs/queue_agent.log &
fi

exit 0
Rob Ruchte
  • 3,569
  • 1
  • 16
  • 18
  • That’s nice @Rob! I might use that next time since cron is easier to setup than supervisord – Chris Haas May 16 '21 at 13:55
  • Thanks, yeah, this is pretty much bulletproof. I've been using it in production on a lot of systems for years. If you have your email address set up as the MAILTO in cron, you'll get the fail log message emailed to you on restart, which usually means that something bad is happening, and if all else fails, you'll get that alert. I use supervisord if I need to maintain a pool of workers, since it lets you configure multiple instances of the same executable, but for keeping a single script running, this just works. – Rob Ruchte May 16 '21 at 14:10
1

I think u can try to figure out why these two php scripts shut down as the first step. To solve this problem, u can use this php function:

void register_shutdown_function(callback $callback[.mixed $parameter]);

which Registers a callback to be executed after script execution finishes or exit() is called. So u can log some info when php files get shut down like this:

function myLogFunction() {
    //log some info here
}
register_shutdown_function(myLogFunction);

Instead of putting the standard output and error output into /dev/null, u can put them into a log file(Since maybe we can get some helpful info from the output). So instead of using:

nohup php file_1.php >/dev/null 2>&1 &
nohup php file_2.php >/dev/null 2>&1 &

try:

nohup php file_1.php 2>yourLog.log &
nohup php file_2.php 2>yourLog.log &

If u want to autorun these two php files when u boot the server, try edit /etc/rc.local(which is autorun when the os start up). Add your php cli command lines in this file.

If u can't figure out why php threads get shut down, try supervisor as @Chris Haas mensioned.