php exec() with timeout

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.

Published
Categorized as PHP

Leave a comment

Your email address will not be published. Required fields are marked *