Score:0

Running PHP script constantly in background

gb flag

I have created a PHP file that contains an infinite loop that does certain checks and acts if variables change (what's inside the script is probably not very important for my question). I want the service to always run, untill I tell it to stop. After some searching I decided to be using nohup for this task. However, the downside of nohup is that I can't really monitor if the service is down (other than manually running ps -aux and looking for the process).

I've also read things about supervisord, which also sounded pretty good, but I'm kind of lost and don't really know what the best solution is. I want the PHP script to automatically start on reboots and I want the service to check if the script is still running, if not, I want it to reboot/alert so I can check what's up.

Should I be using either nohup or supervisord? Or do you guys recommend something totally different? It would be great if I could run the service inside a docker container where I can simply add the execution of the service inside the Dockerfile.

in flag
What's wrong with creating a systemd unit?
in flag
the Docker container would already do what you want, you answered your own question.
Jordy avatar
gb flag
@GeraldSchneider how does a docker container already do that? Does a docker container create a service of the php command it executes? And I'll look into systemd, is it any different than supervisord?
Score:0
in flag

Sounds like you're better off just using good'ol CRON.

You can configure any script to run at whatever interval though I would not recommend running it continuously, maybe have it run every 5 minutes. So something like this:

*/5 * * * * php /path/to/php/file >/dev/null 2>&1

So you can learn more about CRON: https://linuxman.co/linux-server/cron-a-nosedive-into-lazy-sysadmin/

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.