Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to keep a PHP script running as a daemon?

Tags:

php

daemon

What is the best way to keep a PHP script running as a daemon, and what's the best way to check if needs restarting.

I have some scripts that need to run 24/7 and for the most part I can run them using nohup. But if they go down, what's the best way to monitor it so it can be automatically restarted?

like image 889
Aldie Avatar asked Sep 01 '08 23:09

Aldie


People also ask

How do I keep a PHP script running?

You can put a task (such as command or script) in a background by appending a & at the end of the command line. The & operator puts command in the background and free up your terminal. The command which runs in background is called a job. You can type other command while background command is running.

What is a daemon PHP?

A daemon is a Linux program that run in the background, just like a 'Service' on Windows. It can perform all sorts of tasks that do not require direct user input. Apache is a daemon, so is MySQL. All you ever hear from them is found in somewhere in /var/log , yet they silently power over 40% of the Internet.

What is a daemon in Linux?

A daemon is a service process that runs in the background and supervises the system or provides functionality to other processes. Traditionally, daemons are implemented following a scheme originating in SysV Unix.


2 Answers

If you can't use the (proper) init structure to do this (you're on shared hosting, etc.), use cron to run a script (it can be written in whatever language you like) every few minutes that checks to see if they're running, and restarts them if necessary.

like image 111
rmmh Avatar answered Oct 21 '22 15:10

rmmh


The most elegant solution is reactPHP.

like image 40
Janus Troelsen Avatar answered Oct 21 '22 14:10

Janus Troelsen