I've got a PHP script that needs to invoke a shell script but doesn't care at all about the output. The shell script makes a number of SOAP calls and is slow to complete, so I don't want to slow down the PHP request while it waits for a reply. In fact, the PHP request should be able to exit without terminating the shell process.
I've looked into the various exec()
, shell_exec()
, pcntl_fork()
, etc. functions, but none of them seem to offer exactly what I want. (Or, if they do, it's not clear to me how.) Any suggestions?
I also found Symfony Process Component useful for this.
See how it works in its GitHub repo.
php-execute-a-background-process has some good suggestions. I think mine is pretty good, but I'm biased :)
You can also run the PHP script as daemon or cronjob:
#!/usr/bin/php -q
I used this...
(where
PHP_PATH
is a const defined likedefine('PHP_PATH', '/opt/bin/php5')
or similar)It passes in arguments via the command line. To read them in PHP, see argv.
If it "doesn't care about the output", couldn't the exec to the script be called with the
&
to background the process?EDIT - incorporating what @AdamTheHut commented to this post, you can add this to a call to
exec
:That will redirect both
stdio
(first>
) andstderr
(2>
) to/dev/null
and run in the background.There are other ways to do the same thing, but this is the simplest to read.
An alternative to the above double-redirect:
In Linux, you can start a process in a new independent thread by appending an ampersand at the end of the command
In Windows, you can use the "start" DOS command