I've written a program that is suppose to run for a long time and it outputs the progress to stdout
, however, under some circumstances it begins to hang and the easiest thing to do is to restart it.
My question is: Is there a way to do something that would kill the process only if it had no output for a specific number of seconds?
I have started thinking about it, and the only thing that comes to mind is something like this:
./application > output.log &
tail -f output.log
then create script which would look at the date and time of the last modification on output.log
and restart the whole thing.
But it looks very tedious, and i would hate to go through all that if there were an existing command for that.
As far as I know, there isn't a standard utility to do it, but a good start for a one-liner would be:
timeout=10; if [ -z "`find output.log -newermt @$[$(date +%s)-${timeout}]`" ]; then killall -TERM application; fi
At least, this will avoid the tedious part of coding a more complex script.
Some hints:
find
utility to compare the last modification date of the output.log file against a time reference.date
utility as the current time in seconds (+%s
) since EPOCH (1970-01-01 UTC).bash
$[] operation to subtract the $timeout value (10 seconds on the example)find
, then the file wasn't changed for more than 10 seconds. This will trigger a true in the if
condition and the killall
command will be executed.You can also set an alias
for that, using:
alias kill_application='timeout=10; if [ -z "`find output.log -newermt @$[$(date +%s)-${timeout}]`" ]; then killall -TERM application; fi';
And then use it whenever you want by just issuing the command kill_application
If you want to automatically restart the application without human intervention, you can install a crontab
entry to run every minute or so and also issue the application restart command after the killall
(Probably you may also want to change the -TERM to -KILL, just in case the application becomes unresponsive to handleable signals).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With