php 如何检查php脚本是否仍在运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/117226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a php script is still running
提问by Justin Levene
I have a PHPscript that listens on a queue. Theoretically, it's never supposed to die. Is there something to check if it's still running? Something like Ruby's God ( http://god.rubyforge.org/ )for PHP?
我有一个PHP监听队列的脚本。理论上,它永远不应该死。有什么东西可以检查它是否仍在运行?像Ruby's God ( http://god.rubyforge.org/ )为了PHP什么?
God is language agnostic but it would be nice to have a solution that works on windows as well.
上帝是语言不可知论者,但如果有一个也适用于 Windows 的解决方案会很好。
回答by Jay
I had the same issue - wanting to check if a script is running. So I came up with this and I run it as a cron job. It grabs the running processes as an array and cycles though each line and checks for the file name. Seems to work fine. Replace #user# with your script user.
我有同样的问题 - 想检查脚本是否正在运行。所以我想出了这个,我把它作为一个 cron 工作来运行。它将正在运行的进程作为一个数组抓取并循环遍历每一行并检查文件名。似乎工作正常。将#user# 替换为您的脚本用户。
exec("ps -U #user# -u #user# u", $output, $result);
foreach ($output AS $line) if(strpos($line, "test.php")) echo "found";
回答by Justin Levene
In linux run ps as follows:
在linux下运行ps如下:
ps -C php -f
You could then do in a php script:
然后您可以在 php 脚本中执行以下操作:
$output = shell_exec('ps -C php -f');
if (strpos($output, "php my_script.php")===false) {
shell_exec('php my_script.php > /dev/null 2>&1 &');
}
The above code lists all php processes running in full, then checks to see if "my_script.php" is in the list of running processes, if not it runs the process and does not wait for the process to terminate to carry on doing what it was doing.
上面的代码列出了所有正在运行的php进程,然后检查“my_script.php”是否在正在运行的进程列表中,如果没有就运行进程,不等待进程终止继续做它正在做。
回答by troelskn
Just append a second command after the script. When/if it stops, the second command is invoked. Eg.:
只需在脚本后附加第二个命令。当/如果它停止,则调用第二个命令。例如。:
php daemon.php 2>&1 | mail -s "Daemon stopped" [email protected]
Edit:
编辑:
Technically, this invokes the mailer right away, but only completes the command when the php script ends. Doing this captures the output of the php-script and includes in the mail body, which can be useful for debugging what caused the script to halt.
从技术上讲,这会立即调用邮件程序,但仅在 php 脚本结束时才完成命令。这样做会捕获 php-script 的输出并包含在邮件正文中,这对于调试导致脚本停止的原因非常有用。
回答by Mez
Simple bash script
简单的bash脚本
#!/bin/bash
while [true]; do
if ! pidof -x script.php;
then
php script.php &
fi
done
回答by Alister Bulman
Not for windows, but...
不是用于窗户,而是...
I've got a couple of long-running PHP scripts, that have a shell script wrapping it. You can optionally return a value from the script that will be checked in the shell-script to exit, restart immediately, or sleep for a few seconds -and then restart.
我有几个长期运行的 PHP 脚本,它们有一个封装它的 shell 脚本。您可以选择从脚本中返回一个值,该值将在 shell 脚本中检查以退出、立即重新启动或休眠几秒钟 - 然后重新启动。
Here's a simple one that just keeps running the PHP script till it's manually stopped.
这是一个简单的脚本,它一直运行 PHP 脚本,直到它被手动停止。
#!/bin/bash clear date php -f cli-SCRIPT.php echo "wait a little while ..."; sleep 10 exec0 3 * * * /usr/bin/php -f /home/test/test.php my_special_cron
The "exec $0" restarts the script, without creating a sub-process that will have to unravel later (and take up resources in the meantime). This bash script wraps a mail-sender, so it's not a problem if it exits and pauses for a moment.
“exec $0”会重新启动脚本,而不会创建稍后必须解开的子进程(并同时占用资源)。这个 bash 脚本包装了一个邮件发件人,所以如果它退出并暂停片刻就没有问题。
回答by Zvonimir Buri?
You can write in your crontab something like this:
你可以在你的 crontab 中这样写:
<?php
php_sapi_name() == 'cli' || exit;
if($argv[1]) {
substr_count(shell_exec('ps -ax'), $argv[1]) < 3 || exit;
}
// your code here
Your test.php file should look like this:
您的 test.php 文件应如下所示:
$daemonPath = "FULL_PATH_TO_DAEMON";
$runningPhpProcessesOfDaemon = (int) shell_exec("ps aux | grep -c '[p]hp ".$daemonPath."'");
if ($runningPhpProcessesOfDaemon === 0) {
shell_exec('php ' . $daemonPath . ' > /dev/null 2>&1 &');
}
That way you will have only one active instace of the cron job with my-special-cronas process key. So you can add more jobs within the same php file.
这样,您将只有一个 cron 作业的活动实例my-special-cron作为进程键。因此,您可以在同一个 php 文件中添加更多作业。
test.php system_send_emails sendEmails
test.php system_send_emails sendEmails
test.php system_create_orders orderExport
test.php system_create_orders orderExport
回答by Burak Kurkcu
Inspired from Justin Levene's answer and improved it as ps -Cdoesn't work in Mac, which I need in my case. So you can use this in a php script (maybe just before you need daemon alive), tested in both Mac OS X 10.11.4 & Ubuntu 14.04:
灵感来自 Justin Levene 的答案,并改进了它,因为ps -C它在 Mac 中不起作用,这是我需要的。所以你可以在 php 脚本中使用它(也许就在你需要守护进程之前),在 Mac OS X 10.11.4 和 Ubuntu 14.04 中测试:
#!/bin/bash
php test.php
Small but useful detail: Why grep -c '[p]hp ...'instead of grep -c 'php ...'?
小而有用的细节:为什么grep -c '[p]hp ...'而不是grep -c 'php ...'?
Because while counting processes grep -c 'php ...'will be counted as a process that fits in our pattern. So using a regex for first letter of php makes our command different from pattern we search.
因为 while 计数过程grep -c 'php ...'将被视为适合我们模式的过程。因此,对 php 的第一个字母使用正则表达式会使我们的命令与我们搜索的模式不同。
回答by Hyman Simth
If you're having trouble checking for the PHP script directly, you can make a trivial wrapper and check for that. I'm not sufficiently familiar with Windows scripting to put how it's done here, but in Bash, it'd look like...
如果您在直接检查 PHP 脚本时遇到问题,您可以制作一个简单的包装器并检查它。我对 Windows 脚本编写不够熟悉,无法在这里说明它是如何完成的,但是在 Bash 中,它看起来像......
wrapper_for_test_php.sh
wrapper_for_test_php.sh
#!/bin/bash
clear
date
while true
do
php -f processEmails.php
echo "wait a little while for 5 secobds...";
sleep 5
done
Then you'd just check for the wrapper like you'd check for any other bash script: pidof -x wrapper_for_test_php.sh
然后你只需像检查任何其他 bash 脚本一样检查包装器:pidof -x wrapper_for_test_php.sh
回答by Kamaro Lambert
I have used cmder for windows and based on this script I came up with this one that I managed to deploy on linux later.
我在 windows 上使用了 cmder,并基于这个脚本,我想出了这个我后来设法在 linux 上部署的脚本。
$runningScripts = shell_exec('ps -ef |grep '.strtolower($parameter).' |grep '.dirname(__FILE__).' |grep '.basename(__FILE__).' |grep -v grep |wc -l');
if($runningScripts > 1){
die();
}
回答by Dom DaFonte
Here is what I did to combat a similar issue. This helps in the event anyone else has a parameterized php script that you want cron to execute frequently, but only want one execution to run at any time. Add this to the top of your php script, or create a common method.
这是我为解决类似问题所做的工作。如果其他人有一个参数化的 php 脚本,您希望 cron 经常执行,但只想在任何时候运行一个执行,这会有所帮助。将此添加到您的 php 脚本的顶部,或创建一个通用方法。
##代码##
