Ok, I need to write a code that calls a script, and if the operation in script hangs, terminates the process.
The preferred language is Python, but I’m also looking through C and bash script documentation too.
Seems like an easy problem, but I can’t decide on the best solution.
From research so far:
- Python: Has some weird threading model where the virtual machine uses
one thread at a time, won’t work? - C: The preferred solution so far seems to use SIGALARM + fork +
execl. But SIGALARM is not heap safe, so it can trash everything? - Bash: timeout program? Not standard on all distros?
Since I’m a newbie to Linux, I’m probably unaware of 500 different gotchas with those functions, so can anyone tell me what’s the safest and cleanest way?
In bash you can do something similar to this:
Example:
you can even try to use
trap 'script_stopped $pid' SIGCHLD– see the bash man for more info.UPDATE: I found other command timeout. It does exactly what you need – runs a command with a time limit. Example:
will kill the
sleepafter 10 seconds.