I recently had problems with a Python-script executed from a PHP-script obiously sometimes not terminating for some reason. Since the PHP-script itself was called via CLI, this lead to a deadlock of the whole chain.
Even PHP 8.x still does not have an inbuilt function to execute an external command and terminate that when a given timeout is met. I was a bit astonished as I thought this to be a rather basic function.
There is an approach to work-around this here, which does it’s job quite nice (thanks Erik for putting this online, btw!).
However, this function is not parameter-compatible to the original exec() and the output is also not an array as exec() does. So I changed and appended the code a bit to meet my requirements.
You can find the changed code on my GitHub-account as a Gist.
I had the same issue (python sometimes not finish under php).
why not using command timeout? (under Linux/POSIX OS)
“timeout 10 abcd” check “man timeout”
That might also be possible, but IMHO it’s better to handle timeoute on the controlling side rather that the called process. This gives full control when doing exception-handling, because the code “knows” the exact cause of the termination.
Thank you so much for sharing this !
You’re welcome!