使用 PHP 启动/重启进程 [英] Use PHP to start/restart process

查看:86
本文介绍了使用 PHP 启动/重启进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个 php 脚本,它允许我单击一个按钮来重新启动一个 PHP 脚本.但是,我不确定最好的方法.这是它的快照:http://i51.tinypic.com/2niz32o.png>

我目前有这个:

if(isset($_POST['login_restart'])){$command = exec("/usr/bin/php/var/www/html/login_server.php >/dev/null &");$pid = exec("nohup $command >/dev/null 2>&1 & echo $!");$info = "登录服务器已启动...PID: $pid";}

然而,这似乎不起作用.我需要它,所以当按下重新启动"按钮时,它会启动登录服务器并使其保持运行.我一直在 SSH 中使用 screen 功能,但是,我不想一直登录 SSH 来重新启动登录服务器.我想以某种方式使用进程 ID,以便我可以检查脚本是否正在运行,如果没有,它允许我单击重新启动"按钮.

谢谢.

解决方案

您是否有特殊原因想要手动而不是自动执行此操作?服务器不是应该一直重启吗?

我的建议是自动执行此操作,方法是使用 cron 定期检查脚本的状态,或者使用 bash 无限循环脚本永生.

为了方便起见,首先创建一个启动器脚本来调用你的 PHP,并将其命名为 run_login_server.sh(不要忘记 chmod +x 它以便它可以执行):

#!/bin/bash/usr/bin/php/var/www/html_login_server.php >/开发/空

然后创建 login_server_daemon.sh 以无限循环运行您的脚本(再次,chmod +x 使其可执行):

#!/bin/bash尽管 :做./run_login_server.sh # 或任何命令行永远执行完毕

注意我没有在上面的 bash 脚本中对 php 进程进行后台处理.它有效,因为 bash 循环每次都会调用 php,并且循环只会在 php 死亡后再次迭代.只需执行 login_server_daemon.sh 即可启动循环(通过 init 服务或在您现在使用的分离屏幕会话中).

如果您的 PHP 脚本挂起,或者您因为更新了代码而想重新加载它们,您可以简单地终止循环进程——run_login_server.sh 和 bash循环会重新生成它.

它就像killall run_login_server.sh 一样简单,你可以通过php 的exec 来完成.请注意,您需要注意谁执行了什么的用户权限:如果您以 your_username 身份执行 login_server_daemon.sh 但 php 以 php_username 身份运行,那么 php 将没有权限killall 你的进程.

最后,如果您不能在 cronscript 方法之间做出选择,这里有一些因素需要考虑:

脚本应该永远存在,并且只有在 1) 明确杀死,2) bash 以某种方式在 while 循环中绊倒并死亡,我怀疑会发生,以及 3) 发生机器范围的灾难时才会死,在这种情况下你的小 bash 脚本停止是你最不担心的.该脚本的一个好处是在 php(或您想在无限循环中调用的任何内容)死后立即重新启动.

cron 有一个问题,如果你真的关心立即恢复,它只能在最频繁的设置下每分钟检查一次.另一个烦恼是,如果您决定停止脚本,还必须将其从 crontab 中删除,否则它会恢复正常.

I've created a php script that allows me to click a button to restart a PHP script. However, I'm not sure the best way to do it. Here's a snapshot of it: http://i51.tinypic.com/2niz32o.png

I currently have this:

if(isset($_POST['login_restart']))
{
    $command = exec("/usr/bin/php /var/www/html/login_server.php >/dev/null &");
    $pid = exec("nohup $command > /dev/null 2>&1 & echo $!");
    $info = "Login server started...PID: $pid";
}

However, that doesn't seem to work. I need it so when the "Restart" button is pressed, it starts the login server, and keeps it running. I've been using the screen function in SSH, however, I don't want to have to keep logging into SSH to restart the login server. I want to somehow use a process ID so I can check to see if the script is running, and if it's not, it'll allow me to click the "Restart" button.

Thanks.

解决方案

Is there a particular reason that you want to do this manually and not automatically? Is it not the case that the server should always be restarted?

My advice would be to automate this, either by using cron to check the status of your script at regular intervals, or bash infinite loop script immortality.

First create a launcher script to invoke your PHP for convenience, and call it run_login_server.sh (don't forget to chmod +x it so it can be executed):

#!/bin/bash
/usr/bin/php /var/www/html_login_server.php > /dev/null

Then create login_server_daemon.sh to run your script in an infinite loop (again, chmod +x it to make it executable):

#!/bin/bash
while :
do
  ./run_login_server.sh # or any command line to be executed forever
done

N.B. I have not backgrounded the php process in the above bash script. It works, because the bash loop will call php each time, and the loop will only iterate again once php has died. Just execute login_server_daemon.sh to start the loop (either through an init service or in a detached screen session like you are using now).

If your PHP scripts hang, or you want to reload them because you have updated your code, you can simply kill the looped process–run_login_server.sh and the bash loop will respawn it.

It's as simple as killall run_login_server.sh, which you could do via php's exec. Note that you need to be careful about the user permissions of who has executed what: if you execute login_server_daemon.sh as your_username but php runs as php_username then php will not have permission to killall your process.

Finally, if you can't choose between cron and the script approaches, here are some factors to consider:

The script should live forever, and will only die if 1) explicitly killed, 2) bash somehow trips and dies on a while loop, which I doubt would happen, and 3) a machine-wide catastrophe happens, in which case your little bash script stopping is the least of your worries. A bonus with the script is that restart is immediate after php (or whatever you want to call in the infinite loop) dies.

cron has a the problem that it can only check once a minute at its most frequent setting, if you really care about immediate recovery. It has the additional annoyance that if you decide to stop the script, you also have to remove it from your crontab or it will just come back to life.

这篇关于使用 PHP 启动/重启进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆