15

我开始了这个过程(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      = "xxxxxxxxx@gmail.com";
    $subject = "Hello Richie";
    $header  = "From: me@xxxxxxxxx.org";
    $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); 
?>

但我现在想阻止它。它正在向我的 Gmail a/c 发送数百封邮件。它在一个网络服务器上,我无法重新启动它来终止进程。

有没有办法执行另一个文件来终止进程,或者我该怎么做?

4

6 回答 6

22

如果您没有 shell 访问权限,那么唯一的方法是要求服务器管理员终止该进程。

话虽如此,有一种简单的方法可以设置脚本并使其能够从同一服务器上的任何其他脚本中取消,如下所示:

<?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');

?>
于 2014-12-04T14:43:11.537 回答
8

我假设您也没有外壳访问权限。我怀疑最简单的方法是联系管理员并让他们重新启动 Apache。他们比你更不想让这个运行。

一种替代方法是尝试使用 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); 
        }
    }
}
?>
于 2012-12-12T19:59:32.020 回答
5

以下将停止后台进程(PHP):

sudo service apache2 stop

再次启动 apache:

sudo service apache2 start

要简单地重新启动 apache:

sudo service apache2 start
于 2015-05-02T11:22:28.567 回答
4

如果你在后台启动它ps aux | grep time.php来获取 PID。那么就kill PID. 如果进程在前台启动,使用中断它。

如果进程在 Apache 中启动,请重新启动 Apache(Debian 的 /etc/init.d/apache2 restart)。如果进程使用 CGI 类服务器启动,请重新启动服务器。

于 2012-12-12T19:52:47.983 回答
3

要停止运行 php 脚本,您只需在终端中输入以下命令。这将重新启动 Apache 服务。(最终重新启动您的 php 服务)。

sudo service apache2 restart

您可能希望将来在您的脚本中包含超时。

于 2015-02-19T16:36:33.977 回答
0

您可以终止正在运行的 apache 进程。尝试运行类似的东西apachectl stop,然后apachectl start重新启动它。这将在一段时间内终止您的服务器,但它还将确保卡在该脚本上的任何进程都会消失。

于 2012-12-12T19:52:20.323 回答