shell exec - If PHP shell_exec() works too long or executed program hangs... how to catch error? -
If I execute shell_exec ()
with an external program (Unix) and more than that Works 30 seconds, PHP dies with fatal error. This is the reason why the external program hangs up / crashes or I do not know.
I want to catch this error. Try {} .. hold {}
does not work here. How can I tell that the external program is hanged? Usually my external programs run under 2 seconds.
You can use this function system_with_timeout
"run-test. Defined in the "FTP" script, included in source distribution:
(the key is sent to stream_select
)
function system_with_timeout ( $ Commandline, $ env = null, $ stdin = null) {global $ leak_check, $ cwd; $ Data = b ''; $ Bin_env = Array (); Forex (array) $ env $ key = & gt; $ Value) {$ bin_env [(binary) $ key] = (binary) $ value; } $ Proc = proc_open ($ commandline, array (0 = & gt; array ('pipe', 'r'), 1 = & gt; array ('pipe', 'wa'), 2 = & gt; array 'Pip', 'wa'), $ pipes, $ cwd, $ bin_env, array ('suppress_errors' => true, 'binary_psips' => true)); If (! $ Proc) {return false; } If (! Is_null ($ stdin)) {fwrite ($ pipe [0], (binary) $ stdin); } Fclose ($ pipe [0]); $ Timeout = $ leak_check? 300: (project ($ env ['TEST_TIMEOUT'])? $ Env ['TEST_TIMEOUT']: 60); While (true) {/ * hidden errors from SYScalls * / $ r = $ pipes; $ W = faucet; $ E = Null; $ N = @stream_select ($ r, $ w, $ e, $ timeout); If ($ n === incorrect) {break; } And if ($ n === 0) {/ * time out / / $ data = B "\ n ** Error: Process timed out ** \ n"; Proc_terminate ($ proc); $ Return data; } And if ($ n & gt; 0) {$ line = (binary) Fred ($ pipe [1], 8192); If (strølen ($ line) == 0) {/ * EOF * / break; } $ Data. = $ Line; }} $ Stat = proc_get_status ($ proc); If ($ stat ['signal']) {$ data. B = "\ nTermsig =" $ stat ['stopsig']; } $ Code = proc_close ($ proc); $ Return data; }
The timeline is given by passing an array like this: array ('TEST_TIMEOUT' => 200)
as the second parameter.
Comments
Post a Comment