Is there a way to use shell_exec without waiting f

2019-01-01 05:30发布

I have a process intensive task that I would like to run in the background.

The user clicks on a page, the PHP script runs, and finally, based on some conditions, if required, then it has to run a shell script, E.G.:

shell_exec('php measurePerformance.php 47 844 email@yahoo.com');

Currently I use shell_exec, but this requires the script to wait for an output. Is there any way to execute the command I want without waiting for it to complete?

标签: php shell
9条回答
伤终究还是伤i
2楼-- · 2019-01-01 05:35

How about adding.

"> /dev/null 2>/dev/null &"

shell_exec('php measurePerformance.php 47 844 email@yahoo.com > /dev/null 2>/dev/null &');

Note this also gets rid of the stdio and stderr.

查看更多
弹指情弦暗扣
3楼-- · 2019-01-01 05:35

This will execute a command and disconnect from the running process. Of course, it can be any command you want. But for a test, you can create a php file with a sleep(20) command it.

exec("nohup /usr/bin/php -f sleep.php > /dev/null 2>&1 &");
查看更多
情到深处是孤独
4楼-- · 2019-01-01 05:39

If it's off of a web page, I recommend generating a signal of some kind (dropping a file in a directory, perhaps) and having a cron job pick up the work that needs to be done. Otherwise, we're likely to get into the territory of using pcntl_fork() and exec() from inside an Apache process, and that's just bad mojo.

查看更多
若你有天会懂
5楼-- · 2019-01-01 05:46

Use PHP's popen command, e.g.:

pclose(popen("start c:\wamp\bin\php.exe c:\wamp\www\script.php","r"));

This will create a child process and the script will excute in the background without waiting for output.

查看更多
若你有天会懂
6楼-- · 2019-01-01 05:47

That will work but you will have to be careful not to overload your server because it will create a new process every time you call this function which will run in background. If only one concurrent call at the same time then this workaround will do the job.

If not then I would advice to run a message queue like for instance beanstalkd/gearman/amazon sqs.

查看更多
听够珍惜
7楼-- · 2019-01-01 05:47

You could always post over some AJAX to a page that calls the shell_exec().

查看更多
登录 后发表回答