5

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.

2 Answers 2

3

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).

2

The inotifywait could help here, it efficiently waits for changes to files. The exit status can be checked to identify if the event (modify) occurred in the specified interval of time.

$ inotifywait -e modify -t 10 output.log 
Setting up watches.
Watches established.
$ echo $?
2

Some related info from man:

OPTIONS
   -e <event>, --event <event>
       Listen for specific event(s) only.

   -t <seconds>, --timeout <seconds>
       Exit  if  an  appropriate  event  has  not occurred within <seconds> seconds.

EXIT STATUS
   2  The -t option was used and an event did not occur in the specified interval of time.

EVENTS
   modify A watched file or a file within a watched directory was written to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.