加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何停止在后台运行的PHP脚本

发布时间:2020-12-13 14:07:27 所属栏目:PHP教程 来源:网络整理
导读:我开始这个过程(time.php) ?phpignore_user_abort(true); // run script in backgroundset_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 = "xx
我开始这个过程(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,nn"
             . "I was just testing this servicenn "
             . "Thanks! ";

    mail($to,$subject,$body,$header);

    sleep($interval); // wait 5 minutes

} while(true); 
?>

但我现在想阻止它.它发送数百邮件到我的Gmail a / c.它在一个Web服务器上,我不能重新启动它来杀死进程.

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

如果没有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');

?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读