There is a command (say $run_command), which has to be killed after timeout. The solution is quite simple – I can just use timeout from coreutils or timeout3 from other stackoverflow topics. But my command receives it’s stdin from untar pipe smth like this
tar -xO -f "$1" | /usr/bin/time -f "%e" --output=$time_output -- $run_command
Where $run-command is my command, which execution time has also be measured (using the time utility).
So, the question is what is the best way to avoid including untarring time in the timeout utility?
In very simple form, you can kill the calling script like this:
Of course, this is only an example; you’ll want to provide some insurance that $$ is still the same script when
killruns.This will of course kill the whole script, including
time(which will be $timeout_period if the script was killed).UPDATE #1:
Example using a tempfile.
Note that this is still subject to error due to the speed/performance of your filesystem.
UPDATE #2:
This adds the timeout function in addition to exempting
tartime:The same potential issue in the first script segment exists; you’ll want to provide insurance that $$ is still what you think it before you kill it. Also, the signal will be sent to the shell wrapper, not directly to your command. You’ll have to test whether signals get passed through to your command as expected.
Note also that this backgrounds the timeout/kill. The “wait” tells the script to wait until the first background process is finished, so either your command finishes by itself or it gets killed by the timeout … and then the script proceeds to anything after the
wait. If the command finishes by itself, then that’s where you run into potential issues with $pid being recycled for another process. Solving that is left as an exercise for the reader. 🙂