用我的本地服务器上的php运行bash脚本
问题描述:
我知道我可以使用php运行bash命令,但它超时,我需要它连续运行。所以,我所希望实现的是有这个网站叫我的服务器上我的一个bash文件,而不是实际的PHP脚本开始运行它在PHP用我的本地服务器上的php运行bash脚本
答
使用此代码:
<?php
ini_set('max_execution_time', 0);
...
其中0
意味着永远运行/直到脚本结束。或者以秒为单位设置合理的限制。
答
这是我使用通过PHP运行的bash脚本:
<?php
set_time_limit(0); // run for unlimited time
ignore_user_abort(1); //runs even if the user closes the browser/shell session
$command = $_GET['c'];
$pass = $_GET['p'];
if($p == "my password" & !EMPTY($command)){
switch ($command){
case "dothis":
$command = "sh /path/to/dothis.sh";
break;
case "dothat":
$command = "sh /path/to/dothat.sh";
break;
default:
$command = "";
break;
}
shell_exec("nohup $command &");
// nohup: run a command immune to hangups, with output to a non-tty
}
?>
例如:http://www.my-site.com/bash.php?c=dothis&p=mypass
您还可以密码通过apache
.htaccess/.htpasswd
保护的目录,然后,您可以使用访问脚本:
很可能它不是运行到超时的bash脚本,但控制PHP请求。所以你寻找一种解决方法。因此,要么提高您为php配置的最大执行时间,要么使用'nohup'实用程序从控制进程中分离您的bash脚本。见'man nohup'。 – arkascha
使它永远运行*对网络请求没有意义。除非实际需要捕获脚本输出,否则只需将其缓存以供稍后执行('batch'或'at')。 “ – mario
”连续运行 – samrap