在命令行PHP-CLI中重新加载PHP文件
我有一个像下面这样的PHP文件,我使用下面的命令在命令提示符下运行它在命令行PHP-CLI中重新加载PHP文件,php,command-line-interface,Php,Command Line Interface,我有一个像下面这样的PHP文件,我使用下面的命令在命令提示符下运行它 php myfile.php 它工作得很好。我想保持24/7运行它,但有时如果出现任何错误,它会停止工作,所以我想在相同的命令提示符下重新加载文件。我在centos中使用它,我希望在命令提示符中看到结果,所以Cron作业对我来说没有用处。我想把代码,可以停止文件运行,并重新启动它 <?php ini_set('memory_limit', '-1'); set_time_limit(0); require_once
php myfile.php
它工作得很好。我想保持24/7运行它,但有时如果出现任何错误,它会停止工作,所以我想在相同的命令提示符下重新加载文件。我在centos中使用它,我希望在命令提示符中看到结果,所以Cron作业对我来说没有用处。我想把代码,可以停止文件运行,并重新启动它
<?php
ini_set('memory_limit', '-1');
set_time_limit(0);
require_once ('src/whats.class.php');
$starttime = time();
global $w;
connectwa();
function onGetMessage($object)
{
global $w;
$message = $object->getMessage();
$contact = explode("@", $object->getFrom()) [0];
$type = "You have sent *Simple Text* Message.";
sendMessage($contact, $type, $message);
}
function connectwa() {
global $w;
$wait = mt_rand(10, 15);
global $waittime;
$waittime = $wait*60;
echo $waittime;
$log = false;
$debug = false;
$nickname = 'Number1';
$username = 'Your Number will be Here';
$password = 'Your Password will be here';
$w = new Whats($username, $nickname, $debug, $log);
$w->eventManager()->bind('onGetMessage', 'onGetMessage');
try {
$w->connect();
}
catch(Exception $e) {
echo 'Connection error: ' . $e->getMessage();
exit(0);
}
try {
$w->loginWithPassword($password);
}
catch(Exception $e) {
echo 'Login error: ' . $e->getMessage();
exit(0);
}
echo "connected \n";
}
while (1) {
try {
$w->pollMessage();
if (time() - $starttime > $waittime) {
echo "Disconnecting\n";
$starttime = time();
$w->disconnect();
}
}
catch(Exception $e) {
}
if (!$w->isConnected()) {
echo "disconnected\n";
system('clear');
connectwa();
}
}
?>
让我知道有类似的代码可以重新加载我的当前文件。
谢谢在while循环中使用try/catch块。这样,脚本不会因错误或异常而中断。您也可以使用catch块来记录错误。脚本将永远持续运行 注意
如果这是共享主机/服务器,则应用时间限制可能不会产生任何影响。脚本将在设定的时间后中断。但我希望,您是在您的个人/专用服务器上执行此操作的 我多次这样做是为了保持PHP脚本运行,并确保它在退出时继续重新运行。更重要的是,如果他们开始跑掉而出现未预料到的错误,就能够放慢速度。在PHP7+中,捕获最基本的异常(a
\Throwable
确保捕获所有可能抛出的异常)
传递给shell脚本的任何参数都会传递到PHP脚本中,当PHP文件退出时,shell脚本会重新运行自身(不是递归地,而是替换自身)以再次运行。在PHP脚本中,您可以随意循环,但是因为重新启动它是如此简单和快速,我可能会在故意退出之前执行50-100次循环来清理任何东西
我进一步扩展了这个简单的三行脚本,添加了一些选项,可以与exit$n进行通信代码>:
#!/bin/bash
# a shell script that keeps looping until an exit code is given
# if it does an exit(0), restart after a second - or if it's a declared error
# if we've restarted in a planned fashion, we don't bother with any pause
# and for one particular code, exit the script entirely.
# The numbers 97, 98, 99 must match what is returned from the PHP script
nice php -q -f ./cli-beanstalk-worker.php -- $@
ERR=$?
## Possibilities
# 97 - planned pause/restart `exit 97;`
# 98 - planned restart (immediate, no sleep)
# 99 - planned stop, exit.
# 0 - unplanned restart (as returned by "exit;" or exceptions)
# - Anything else is also unplanned paused/restart
if [ $ERR -eq 97 ]
then
# a planned pause, then restart
echo "97: PLANNED_PAUSE - wait 2";
sleep 2;
exec $0 $@;
fi
# ... other choices to potentially pause, or exit the shell script
# unplanned exit, pause, and restart
echo "unplanned restart: err:" $ERR;
echo "sleeping for 5 secs"
sleep 5
# rerun this shell script, replacing itself
exec $0 $@
你能抓住,抓住错误吗?您在catch块中做什么?@riggsfully我正在使用catch(异常$e),当出现任何错误时,我希望重新加载我的文件。谢谢当出现错误时,您通过“停止当前文件并希望再次运行它”来具体实现什么?为什么不能让脚本继续运行?@PatrickQ是基于套接字连接的文件,因此有时会因为一些错误而停止,当我们再次运行它时,它就开始工作了,所以我正在寻找在出现任何错误并且文件停止时自动再次运行文件的命令。为什么不在catch中关闭并重新打开连接呢。i、 e.清理并重新开始处理。那么脚本就不需要重新启动“在while循环中使用try/catch块”OP已经在这样做了!我在私有VPS中使用它,我已经使用了catch(例外$e)。我希望在出现错误时重新加载文件。感谢如果您的脚本与try/catch不相上下,那么错误可能出现在其他地方。尝试将IF块放置在try块内。或者看看发生了什么行错误,尝试将该脚本包围到try/catch块中。@MalikAhmedKhanAwan我基本上是在寻找可以重新加载文件的命令代码。这会解决我的问题。类似于系统(“清晰”);谢谢你的回答。它的Bash脚本?我是Linux新手。我需要在我的PHP文件中包含这段代码吗?谢谢这个代码是额外的,你可以运行它。它运行php文件,并在失败时重新启动它。我在博客上写过运行这样的系统,不过这是为了处理队列,而不是处理web套接字,正如您可能正在处理的那样。
#!/bin/bash
nice php -q -f ./myfile.php -- $@
exec $0 $@
#!/bin/bash
# a shell script that keeps looping until an exit code is given
# if it does an exit(0), restart after a second - or if it's a declared error
# if we've restarted in a planned fashion, we don't bother with any pause
# and for one particular code, exit the script entirely.
# The numbers 97, 98, 99 must match what is returned from the PHP script
nice php -q -f ./cli-beanstalk-worker.php -- $@
ERR=$?
## Possibilities
# 97 - planned pause/restart `exit 97;`
# 98 - planned restart (immediate, no sleep)
# 99 - planned stop, exit.
# 0 - unplanned restart (as returned by "exit;" or exceptions)
# - Anything else is also unplanned paused/restart
if [ $ERR -eq 97 ]
then
# a planned pause, then restart
echo "97: PLANNED_PAUSE - wait 2";
sleep 2;
exec $0 $@;
fi
# ... other choices to potentially pause, or exit the shell script
# unplanned exit, pause, and restart
echo "unplanned restart: err:" $ERR;
echo "sleeping for 5 secs"
sleep 5
# rerun this shell script, replacing itself
exec $0 $@