Entry
How can I run a process from a script and not wait for it to finish?
How can I start a background process from a PHP script?
Jan 30th, 2001 01:00
Marko Vukovic, Manuel Lemos, Nathan Wallace,
On one of the unices you can do:
e.g.
exec("commmand&");
exec("perl file.pl&");
The "&" operator on the end will run the program in the background.
See UNIX NOTICE below.
For win nt/95 you could use the "start" command:
exec("start /xxxxx file.exe");
Start by default does not "wait" until the command has finished unless
you do start /w (which you don't want).
Winnt has lots of fine options (start /? for help) to limit ammount of
ram, etc and even to run without a widow.
UNIX NOTICE: On UNIX systems, PHP uses the function popen() to
implement
the functions exec(), system() and passthru(). In this case popen()
returns a stream handle for the command output. This handle has to be
closed with pclose() function when the command exits.
If you start one or more programs in the background within the executed
script without redirecting its output, pclose() will hang until all
programs started to run in the background from this script exit.
This may be a problem if you do not want to wait until the background
programs exits. To avoid this problem, simply redirect the background
programs output to some file or simply to nowhere (/dev/null) like this:
system("mydeamon >/dev/null");
--
I found that I still had to add the &, like this:
system("mydaemon >/dev/null &");