Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill a process on no output for some period of time

Tags:

linux

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.

like image 463
v010dya Avatar asked Jun 22 '16 03:06

v010dya


1 Answers

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:

  • Using the find utility to compare the last modification date of the output.log file against a time reference.
  • The time reference is returned by date utility as the current time in seconds (+%s) since EPOCH (1970-01-01 UTC).
  • Using bash $[] operation to subtract the $timeout value (10 seconds on the example)
  • If no output is returned from the above 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).

like image 70
pah Avatar answered Sep 26 '22 03:09

pah