Run php script as daemon process

后端 未结 14 1590
-上瘾入骨i
-上瘾入骨i 2020-11-22 11:36

I need to run a php script as daemon process (wait for instructions and do stuff). cron job will not do it for me because actions need to be taken as soon as instruction arr

14条回答
  •  情深已故
    2020-11-22 12:20

    As others have already mentioned, running PHP as a daemon is quite easy, and can be done using a single line of command. But the actual problem is keeping it running and managing it. I've had the same problem quite some time ago and although there are plenty of solutions already available, most of them have lots of dependencies or are difficult to use and not suitable for basic usages. I wrote a shell script that can manage a any process/application including PHP cli scripts. It can be set as a cronjob to start the application and will contain the application and manage it. If it's executed again, for example via the same cronjob, it check if the app is running or not, if it does then simply exits and let its previous instance continue managing the application.

    I uploaded it to github, feel free to use it : https://github.com/sinasalek/EasyDeamonizer

    EasyDeamonizer

    Simply watches over your application (start, restart, log, monitor, etc). a generic script to make sure that your appliation remains running properly. Intentionally it uses process name instread of pid/lock file to prevent all its side effects and keep the script as simple and as stirghforward as possible, so it always works even when EasyDaemonizer itself is restarted. Features

    • Starts the application and optionally a customized delay for each start
    • Makes sure that only one instance is running
    • Monitors CPU usage and restarts the app automatically when it reaches the defined threshold
    • Setting EasyDeamonizer to run via cron to run it again if it's halted for any reason
    • Logs its activity

提交回复
热议问题