检查 Linux 中的进程是否正在使用 PHP 运行
我正在使用 kannel 通过 PHP 发送短信。我想知道如何检查特定进程是否正在运行。为了让 kannel 运行,名为 bearerbox
的进程应该一直运行。我想检查这个进程是否正在运行。因为如果该进程未运行,则会向我发送一封邮件,通知我有关情况。
I am using kannel to send SMS via PHP. I want to know how can I check if a particular process is running. For kannel to run, a process named bearerbox
should be running all time. I want to check if this process is running or not. Because if the process is not running then a mail will be sent to me notifying me about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最简单的方法是使用 pgrep,如果进程存在,它的退出代码为 0,否则为 1。
这是一个例子。
The easiest is to use
pgrep
, which has an exit code of 0 if the process exists, 1 otherwise.Here's an example.
您可以使用 exec 命令查找您的进程,然后采取相应的操作。
类似于:
exec('ps aux | grep bearerbox', $output);
您需要计算出服务器上返回的内容,以确定它是否正在运行。
祝你好运。
You can use the exec command to find your process and then act accordingly.
Something like:
exec('ps aux | grep bearerbox', $output);
You'll need to work out what is returned on your server to decide if it's running or not.
Good luck.
有很多方法可以解决这个问题。最简单的(也是问题的直接答案)是获取“ps”的输出。
不过,守护进程总是会创建一个“pid”文件。该文件包含守护程序的进程 ID。如果您有该文件,您可以检查该文件的内容并查看具有该 ID 的进程是否仍在运行。这个比较靠谱。
supervisord 也可能具有此功能。最后,也许获得一个真正的监控系统比自己构建一些东西更好。 Nagios 可能是一个不错的选择,但可能还有其他选择。
There are a lot of ways to deal with this. The easiest (and a direct answer to your question) is to grab the output of 'ps'.
Deamons tend to always create a 'pid' file though. This file contains the process-id of the daemon. If yours has that, you can check the contents of the file and see if the process with that id is still running. This is more reliable.
supervisord might also have this functionality. Lastly, maybe it's better to get a real monitoring system rather than build something yourself. Nagios might be a good pick, but there might be others.
通过 PHP 监控进程的简单而方便的解决方案: PHP-Linux-Process-监控。
代码目标如下:
Simple yet handy solution to monitor processes through PHP: PHP-Linux-Process-Monitor.
The code goals like:
这是上面代码中最好的方法
,gs是我正在检查的进程
This is the best way
in the above code gs is the process that I was checking