php shell_exec multiple commands at once in background

两盒软妹~` 提交于 2020-01-14 09:22:29

问题


I have a problem in php/linux, described below:

I have to execute a linux command through shell_exec (plesk cli command subscription webspace-off).

The problem is when i do this from php it works, but restarts apache, that resulting in a blank page, while apache restarts.

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." &");

but php will wait for response.

Somehow i need to sleep the execution of a linux command , and all this has to run in bg., and dont wait for response.

Thanks


回答1:


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 &");

(Note the () around the commands: you need to catch the output stream of both sleep and your wrapper.)

Edit: and make real sure that you validate $domain_name. Without validation and with

$domain_name = "; rm -rf ...";

you're in trouble...



来源:https://stackoverflow.com/questions/6402658/php-shell-exec-multiple-commands-at-once-in-background

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!