php shell_exec在后台一次执行多个命令 [英] php shell_exec multiple commands at once in background
问题描述
我在php/linux中有问题,如下所述:
I have a problem in php/linux, described below:
我必须通过shell_exec(请执行cli命令订阅,请执行webspace-off)来执行linux命令.
I have to execute a linux command through shell_exec (plesk cli command subscription webspace-off).
问题是,当我从php执行此操作时,它可以工作,但是重新启动apache,导致空白页,而apache重新启动.
The problem is when i do this from php it works, but restarts apache, that resulting in a blank page, while apache restarts.
要摆脱这个问题,我必须在后台调用该shell_exec,并要延迟一段时间(预期结果:网页加载,并在4秒钟后运行linux脚本.)
To get rid of the problem i have to call that shell_exec in background, with a delay (Expected result: the web page loads, and after 4 sec. runs the linux script.)
我做了一些尝试,例如:
I have done some tryings like:
shell_exec("sleep 4 && /var/www/vhosts/site.com/httpdocs/wrapper2 3 --webspace-off ".$domain_name." &");
但是php将等待响应.
but php will wait for response.
以某种方式,我需要休眠linux命令的执行,而所有这些都必须在bg中运行. 不要等待回应.
Somehow i need to sleep the execution of a linux command , and all this has to run in bg., and dont wait for response.
谢谢
推荐答案
You should try using exec
rather than shell_exec
, and redirect all output to /dev/null
. Something like:
exec("(sleep 4 && ... --webspace-off ".$domain_name.") > /dev/null 2>&1 &");
(请注意命令周围的()
:您需要捕获sleep
和包装器的输出流.)
(Note the ()
around the commands: you need to catch the output stream of both sleep
and your wrapper.)
真实并确保您确认$domain_name
.未经验证且使用
and make real sure that you validate $domain_name
. Without validation and with
$domain_name = "; rm -rf ...";
您遇到了麻烦...
这篇关于php shell_exec在后台一次执行多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!