保持PHP脚本作为守护进程运行的最佳方法是什么?

时间:2022-11-02 20:07:47

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.

保持PHP脚本作为守护程序运行的最佳方法是什么,以及检查是否需要重新启动的最佳方法是什么。

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?

我有一些需要全天候运行的脚本,大多数情况下我可以使用nohup运行它们。但如果它们发生故障,那么监控它的最佳方法是什么,以便它可以自动重启?

9 个解决方案

#1


4  

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.

如果您不能使用(正确的)init结构来执行此操作(您正在使用共享主机等),请每隔几分钟使用cron运行一个脚本(它可以用您喜欢的任何语言编写)看看它们是否正在运行,并在必要时重新启动它们。

#2


3  

We run our daemons by piping the output to mail.

我们通过将输出传递给邮件来运行我们的守护进程。

php daemon.php | mail -s "daemon stopped" foo@example.org

That way, when/if the daemon stops, it will send a mail, and we will be notified that way.

这样,当守护进程停止时,它将发送邮件,我们将以这种方式得到通知。

It still means manual restart of the daemons of course, but we'll know right away. Usually, if the daemons stopped, it means that there is something else that needs to be taken care of anyway, so that's usually ok.

它仍然意味着手动重启守护进程当然,但我们马上就知道了。通常,如果守护进程停止,则意味着还有其他东西需要照顾,所以通常都可以。

#3


3  

Quick and dirty cron to restart your daemon:

快速而又脏的cron来重启你的守护进程:

* * * * * USER ps auxww | grep SCRIPTNAME > /dev/null || SCRIPTNAME

Replace USER with the user that the daemon runs as and SCRIPTNAME with the name of your script. Stick this in /etc/cron.d/restart_php_daemon. It should run every minute. Change the first * to */2 or */5 to run less frequently.

将USER与守护程序运行的用户替换为,将SCRIPTNAME替换为脚本名称。请将此粘贴在/etc/cron.d/restart_php_daemon中。它应该每分钟运行一次。将第一个*更改为* / 2或* / 5以减少运行频率。

UPDATE

If you're putting this into your own crontab:

如果你把它放到你自己的crontab中:

Run crontab -e and add:

运行crontab -e并添加:

* * * * * ps auxwww | grep SCRIPTNAME > /dev/null || SCRIPTNAME

#4


3  

The most elegant solution is phpdaemon or reactPHP.

最优雅的解决方案是phpdaemon或reactPHP。

#5


1  

I've had success with running a wget and sending the result to /dev/null on a shared server.

我已成功运行wget并将结果发送到共享服务器上的/ dev / null。

#6


1  

Daemon is a linux process that runs in background; apache or mysql are daemons. In a linux environment, we can run a background program using cronjob, but it has some limitations, and in some scenarios it' s not a good idea. For example, using cronjob, we can't control if the previously run has finished yet. So often it's more convenient run a process as a daemon.

守护进程是一个在后台运行的linux进程; apache或mysql是守护进程。在linux环境中,我们可以使用cronjob运行后台程序,但它有一些限制,在某些情况下它并不是一个好主意。例如,使用cronjob,我们无法控制先前的运行是否已完成。因此,作为守护进程运行进程通常更方便。

// Daemonize
$pid = pcntl_fork(); // parent gets the child PID and child gets 0
if($pid){ // if pid is not 0
     // Only the parent will know the PID. Kids aren't self-aware
     // Parent says goodbye!
     print "Parent : " . getmypid() . " exiting\n";
     exit();
}
print "Child : " . getmypid() . "\n";

The code above is taken from very good article about how to create a daemon in php. You can read this at link

上面的代码来自非常好的文章,关于如何在php中创建一个守护进程。你可以在链接上看到这个

#7


0  

I use a PHP-based script to read from a database and send emails out (using the PEAR Mail_Queue library). I run it from within a bash script and based on the returned result (from "exit $status;") either halt, sleep X seconds, or immediately restart. (I also put a check of the load average/sleep into the PHP script to avoid stressing the mail system).

我使用基于PHP的脚本从数据库中读取并发送电子邮件(使用PEAR Mail_Queue库)。我在bash脚本中运行它,并根据返回的结果(从“exit $ status;”)停止,休眠X秒或立即重启。 (我还检查了PHP脚本中的平均负载/睡眠,以避免对邮件系统造成压力)。

If it was for a long-term daemon that had to be continually running, then I agree, it probably would not be the best thing to run this (though I have heard of some socket servers that did run successfully long term), however, PHP 5.3 does also now have improved garbage collection, and if the script is well written enough to not exit unplanned, then memory should be far less of a problem that before.

如果它是一个必须持续运行的长期守护进程,那么我同意,它可能不是最好的运行它(虽然我听说过一些套接字服务器确实长期运行),但是, PHP 5.3现在也改进了垃圾收集,如果脚本写得足够好,不能退出计划外,那么内存应该远远不是以前的问题。

#8


0  

I agree that PHP is not the best tool for this, however I can understand why you want to use PHP so you can reuse components from your application such as database access, and so on.

我同意PHP不是最好的工具,但是我可以理解你为什么要使用PHP,这样你就可以重用应用程序中的组件,比如数据库访问等等。

I had a similar problem and I ended up developing The Fat Controller which is a daemon written in C that can run PHP scripts. It can also run as a multithreaded daemon, running many instances of a script in parallel.

我有一个类似的问题,我最终开发了Fat Controller,这是一个用C编写的可以运行PHP脚本的守护进程。它还可以作为多线程守护程序运行,并行运行脚本的许多实例。

There's more information and use cases here: http://www.4pmp.com/fatcontroller/

这里有更多的信息和用例:http://www.4pmp.com/fatcontroller/

#9


-1  

TBH, PHP probably isn't the best tool for this, really not what it was designed for. I've heard of memory leaks and other bad things happening when you try this. Also bear in mind PHP only has a finite amount of resource ids (for file handles, db connections ect) per execution of a script.

TBH,PHP可能不是最好的工具,真的不是它的设计目标。我听说你尝试这个时会发生内存泄漏和其他不好的事情。还要记住,每次执行脚本时,PHP只有一定数量的资源ID(用于文件句柄,数据库连接等)。

Be better of using something else, maybe python or perl, though I don't have any real experience writing these sorts of apps, but I do know PHP isn't right for what your trying to do.

更好地使用其他东西,可能是python或perl,虽然我没有任何写这些应用程序的真实经验,但我知道PHP不适合你想要做的事情。

#1


4  

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.

如果您不能使用(正确的)init结构来执行此操作(您正在使用共享主机等),请每隔几分钟使用cron运行一个脚本(它可以用您喜欢的任何语言编写)看看它们是否正在运行,并在必要时重新启动它们。

#2


3  

We run our daemons by piping the output to mail.

我们通过将输出传递给邮件来运行我们的守护进程。

php daemon.php | mail -s "daemon stopped" foo@example.org

That way, when/if the daemon stops, it will send a mail, and we will be notified that way.

这样,当守护进程停止时,它将发送邮件,我们将以这种方式得到通知。

It still means manual restart of the daemons of course, but we'll know right away. Usually, if the daemons stopped, it means that there is something else that needs to be taken care of anyway, so that's usually ok.

它仍然意味着手动重启守护进程当然,但我们马上就知道了。通常,如果守护进程停止,则意味着还有其他东西需要照顾,所以通常都可以。

#3


3  

Quick and dirty cron to restart your daemon:

快速而又脏的cron来重启你的守护进程:

* * * * * USER ps auxww | grep SCRIPTNAME > /dev/null || SCRIPTNAME

Replace USER with the user that the daemon runs as and SCRIPTNAME with the name of your script. Stick this in /etc/cron.d/restart_php_daemon. It should run every minute. Change the first * to */2 or */5 to run less frequently.

将USER与守护程序运行的用户替换为,将SCRIPTNAME替换为脚本名称。请将此粘贴在/etc/cron.d/restart_php_daemon中。它应该每分钟运行一次。将第一个*更改为* / 2或* / 5以减少运行频率。

UPDATE

If you're putting this into your own crontab:

如果你把它放到你自己的crontab中:

Run crontab -e and add:

运行crontab -e并添加:

* * * * * ps auxwww | grep SCRIPTNAME > /dev/null || SCRIPTNAME

#4


3  

The most elegant solution is phpdaemon or reactPHP.

最优雅的解决方案是phpdaemon或reactPHP。

#5


1  

I've had success with running a wget and sending the result to /dev/null on a shared server.

我已成功运行wget并将结果发送到共享服务器上的/ dev / null。

#6


1  

Daemon is a linux process that runs in background; apache or mysql are daemons. In a linux environment, we can run a background program using cronjob, but it has some limitations, and in some scenarios it' s not a good idea. For example, using cronjob, we can't control if the previously run has finished yet. So often it's more convenient run a process as a daemon.

守护进程是一个在后台运行的linux进程; apache或mysql是守护进程。在linux环境中,我们可以使用cronjob运行后台程序,但它有一些限制,在某些情况下它并不是一个好主意。例如,使用cronjob,我们无法控制先前的运行是否已完成。因此,作为守护进程运行进程通常更方便。

// Daemonize
$pid = pcntl_fork(); // parent gets the child PID and child gets 0
if($pid){ // if pid is not 0
     // Only the parent will know the PID. Kids aren't self-aware
     // Parent says goodbye!
     print "Parent : " . getmypid() . " exiting\n";
     exit();
}
print "Child : " . getmypid() . "\n";

The code above is taken from very good article about how to create a daemon in php. You can read this at link

上面的代码来自非常好的文章,关于如何在php中创建一个守护进程。你可以在链接上看到这个

#7


0  

I use a PHP-based script to read from a database and send emails out (using the PEAR Mail_Queue library). I run it from within a bash script and based on the returned result (from "exit $status;") either halt, sleep X seconds, or immediately restart. (I also put a check of the load average/sleep into the PHP script to avoid stressing the mail system).

我使用基于PHP的脚本从数据库中读取并发送电子邮件(使用PEAR Mail_Queue库)。我在bash脚本中运行它,并根据返回的结果(从“exit $ status;”)停止,休眠X秒或立即重启。 (我还检查了PHP脚本中的平均负载/睡眠,以避免对邮件系统造成压力)。

If it was for a long-term daemon that had to be continually running, then I agree, it probably would not be the best thing to run this (though I have heard of some socket servers that did run successfully long term), however, PHP 5.3 does also now have improved garbage collection, and if the script is well written enough to not exit unplanned, then memory should be far less of a problem that before.

如果它是一个必须持续运行的长期守护进程,那么我同意,它可能不是最好的运行它(虽然我听说过一些套接字服务器确实长期运行),但是, PHP 5.3现在也改进了垃圾收集,如果脚本写得足够好,不能退出计划外,那么内存应该远远不是以前的问题。

#8


0  

I agree that PHP is not the best tool for this, however I can understand why you want to use PHP so you can reuse components from your application such as database access, and so on.

我同意PHP不是最好的工具,但是我可以理解你为什么要使用PHP,这样你就可以重用应用程序中的组件,比如数据库访问等等。

I had a similar problem and I ended up developing The Fat Controller which is a daemon written in C that can run PHP scripts. It can also run as a multithreaded daemon, running many instances of a script in parallel.

我有一个类似的问题,我最终开发了Fat Controller,这是一个用C编写的可以运行PHP脚本的守护进程。它还可以作为多线程守护程序运行,并行运行脚本的许多实例。

There's more information and use cases here: http://www.4pmp.com/fatcontroller/

这里有更多的信息和用例:http://www.4pmp.com/fatcontroller/

#9


-1  

TBH, PHP probably isn't the best tool for this, really not what it was designed for. I've heard of memory leaks and other bad things happening when you try this. Also bear in mind PHP only has a finite amount of resource ids (for file handles, db connections ect) per execution of a script.

TBH,PHP可能不是最好的工具,真的不是它的设计目标。我听说你尝试这个时会发生内存泄漏和其他不好的事情。还要记住,每次执行脚本时,PHP只有一定数量的资源ID(用于文件句柄,数据库连接等)。

Be better of using something else, maybe python or perl, though I don't have any real experience writing these sorts of apps, but I do know PHP isn't right for what your trying to do.

更好地使用其他东西,可能是python或perl,虽然我没有任何写这些应用程序的真实经验,但我知道PHP不适合你想要做的事情。