Hướng dẫn php exec in background
I am attempting to launch sar and have it run forever via a php script. But for whatever reason it never actually launches. I have tried the following:
However it never launches sar. If I just use:
It works but it just hangs the php script. My understanding that if a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. asked Jul 6, 2015 at 20:07
2 I will assume your running this on a *nix platform. To get php to run something in the background and not wait for the process to finish I would recommend 2 things: First use Example:
nohup means we do not send the "hang up" signal (which kills the process) when the terminal running the command closes.
On another note, if you are using PHP just to call a shell command, you may want to consider other options like Ubuntu's Upstart with no PHP component--if you are using Ubuntu that is.
answered Jul 6, 2015 at 20:20
DJ SipeDJ Sipe 1,11612 silver badges12 bronze badges 1 (PHP 4, PHP 5, PHP 7, PHP 8) exec — Execute an external program Descriptionexec(string Parameterscommand The command that will be executed. output If the result_code If the Return ValuesThe last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function. Returns To get the output of the executed command, be sure to set and use
the ExamplesExample #1 An exec() example
The above example will output something similar to: Returned with status 0 and output: Array ( [0] => cmb ) NotesWarning When allowing user-supplied data to be passed to this function, use escapeshellarg() or escapeshellcmd() to ensure that users cannot trick the system into executing arbitrary commands.
See Also
Arno van den Brink ¶ 13 years ago
dell_petter at hotmail dot com ¶ 13 years ago
__construct($cl=false){ Simon ¶ 7 years ago
krjdev at gmail dot com ¶ 11 months ago
msheakoski @t yahoo d@t com ¶ 18 years ago
ivk ¶ 1 year ago
Farhad Malekpour ¶ 15 years ago
Paul Sommer ¶ 6 years ago
Bob-PHP at HamsterRepublic dot com ¶ 16 years ago
hans at internit dot NO_SPAM dot com ¶ 20 years ago
layton at layton dot tk ¶ 17 years ago
elwiz at 3e dot pl ¶ 12 years ago
(); juan at laluca dot com ¶ 11 years ago
php dot reg at kjpetrie dot co dot uk ¶ 1 month ago
alvaro at demogracia dot com ¶ 11 years ago
Hypolite Petovan ¶ 2 years ago
Martin Lakes ¶ 11 years ago
bahri at bahri dot info ¶ 13 years ago
$pid === false )
dr_jones153 at hotmail dot com ¶ 14 years ago
|