如何停止在后台运行的 PHP 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13847462/
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 do I stop a PHP script running on the background
提问by Richie
I started this process (time.php)
我开始了这个过程(time.php)
<?php
ignore_user_abort(true); // run script in background
set_time_limit(0); // run script forever
$interval = 300; // do every 1 minute...
do
{
// add the script that has to be ran every 1 minute here
// ...
$to = "[email protected]";
$subject = "Hello Richie";
$header = "From: [email protected]";
$body = "Hello Richard,\n\n"
. "I was just testing this service\n\n "
. "Thanks! ";
mail($to, $subject, $body, $header);
sleep($interval); // wait 5 minutes
} while(true);
?>
but I now want to stop it. It's sending hundreds of mail to my Gmail a/c. It's on a web server where I cant restart it to kill the process.
但我现在想阻止它。它正在向我的 Gmail a/c 发送数百封邮件。它位于 Web 服务器上,我无法重新启动它来终止进程。
Is there a way to execute another file to kill the process, or how do I go about?
有没有办法执行另一个文件来终止进程,或者我该怎么做?
回答by Eric P
If you don't have shell access, then the only way is to ask the server administrater to kill the process.
如果您没有 shell 访问权限,那么唯一的方法就是要求服务器管理员终止该进程。
That being said, there is an easy way to set up a script and enable it to be canceled from any other script on the same server, as follows:
话虽如此,有一种简单的方法可以设置脚本并使其能够从同一服务器上的任何其他脚本中取消,如下所示:
<?php
// at start of script, create a cancelation file
file_put_contents(sys_get_temp_dir().'/myscriptcancelfile','run');
// inside the script loop, for each iteration, check the file
if ( file_get_contents(sys_get_temp_dir().'/myscriptcancelfile') != 'run' ) {
exit('Script was canceled') ;
}
// optional cleanup/remove file after the completion of the loop
unlink(sys_get_temp_dir().'/myscriptcancelfile');
// To cancel/exit the loop from any other script on the same server
file_put_contents(sys_get_temp_dir().'/myscriptcancelfile','stop');
?>
回答by Jordan Mack
I am assuming you do not have shell access either. I suspect the easiest way is to contact the administrator and have them restart Apache. They don't want this running anymore than you do.
我假设您也没有 shell 访问权限。我怀疑最简单的方法是联系管理员并让他们重新启动 Apache。他们不想再像你那样运行了。
One alternative would to attempt to kill all the Apache processes using PHP. It will send a kill signal to all Apache processes, except the one it is running under. This might work if the Apache process running under a shared process without setuid(). Attempt at your own risk.
一种替代方法是尝试使用 PHP 杀死所有 Apache 进程。它将向所有 Apache 进程发送一个终止信号,除了它正在运行的进程。如果 Apache 进程在没有 setuid() 的共享进程下运行,这可能会起作用。尝试自担风险。
<?php
$cpid = posix_getpid();
exec("ps aux | grep -v grep | grep apache", $psOutput);
if (count($psOutput) > 0)
{
foreach ($psOutput as $ps)
{
$ps = preg_split('/ +/', $ps);
$pid = $ps[1];
if($pid != $cpid)
{
$result = posix_kill($pid, 9);
}
}
}
?>
回答by Roy M J
The following will stop the background process(PHP):
以下将停止后台进程(PHP):
sudo service apache2 stop
To start apache again:
再次启动 apache:
sudo service apache2 start
To simply re-start apache:
简单地重新启动apache:
sudo service apache2 start
回答by Valera Leontyev
If you started it in background use ps aux | grep time.phpto get PID. Then just kill PID. If process started in foreground, use to interrupt it.
如果您在后台启动它,请使用它ps aux | grep time.php来获取 PID。那么就kill PID. 如果进程在前台启动,则使用中断它。
If process started within Apache, restart Apache (/etc/init.d/apache2 restart for Debian). If process started with CGI-like server, restart the server.
如果进程在 Apache 中启动,请重新启动 Apache(Debian 为 /etc/init.d/apache2 restart)。如果进程使用类 CGI 的服务器启动,请重新启动服务器。
回答by josh.thomson
To stop your php scripts running you can simply enter the following command in your terminal. This will restart the Apache service. (ultimate restarting your php service).
要停止运行 php 脚本,您只需在终端中输入以下命令即可。这将重新启动 Apache 服务。(最终重新启动您的 php 服务)。
sudo service apache2 restart
You may wish to include timeout's within your scripts in future.
您可能希望将来在脚本中包含超时。
回答by Aaron Hathaway
You can kill the apache process running. Try running something like apachectl stopand then apachectl startto start it back up. This will kill your server for a period of time but it will also make sure that any processes stuck on that script will go away.
您可以终止正在运行的 apache 进程。尝试运行类似的东西apachectl stop,然后apachectl start重新启动它。这将在一段时间内杀死您的服务器,但它也将确保卡在该脚本上的任何进程都将消失。

