Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am looking for a tool to keep a task alive (restarting if necessary) for a specified amount of time (in seconds), and then kill it and stop. For example: keep_for 3600 rsync foo bar:faz should for try to synchronize a directory (restarting rsync in case of f.e. dropped connection), but explicitly kill rsync and stop respawning it after an hour.

I tried to write a shell script for that, but it was surprisingly difficult to write, with lots of edge cases (like child process not getting killed, or shell escaping issues). So... maybe there already is a tool for that?

share|improve this question

3 Answers 3

If your system has Upstart, you can create an events file for your script/command and this may do what you want (if not now then possibly in a future version using planned features).

Here are a few bits of information from the events(5) man page:

  • Manual start:

    start on <event>
    Describes on what condition to start this job. Without this section, the job can only be manually started with the initctl(8) command.

  • Respawn:

    respawn
    This sets the daemon, service, and respawn flags for the job. The respawn flag means that the process will be restarted when it ends

  • Respawn limit:

    respawn limit [count [timeout]]
    This configures respawn limits. Respawn limits only apply if the respawn flag is set for the job; setting a limit does not automatically set respawning capability. If the process is respawned more than count times within an interval of timeout seconds, the job will be stopped automatically, and not restarted. The limit defaults to 10 times within 5 seconds.

See Getting Started.

share|improve this answer
    
If I understand the manual correctly, this is a planned feature... also I think that for an ad-hoc command it is slightly too big :-) Still, thank you for suggestion. –  liori May 7 '10 at 22:09

I'd separate the tasks, keeping them in the same script (I thought of this as a C program, but it should be doable as a script).

I'd start with a background subprocess timer to send a signal at the timeout. The signal handler shuts everything down cleanly and exits.

Then start a loop that respawns the desired command whenever it exits without a success (zero) status.

Trying to keep track of both the timeout and the command in one loop will get excessively complex.

share|improve this answer
1  
If I wanted to write a script, I'd ask on stackoverflow. Here I am just looking for a ready solution. –  liori May 8 '10 at 13:42

This question is posed in terms of an implementation. I suggest you re-frame it in terms of what you are trying to accomplish which may yield a more obvious (and elegant) solution.

share|improve this answer
    
I am looking for a generic robust tool to respawn a process for a given time, and then to forcefully stop it after some time. Downloading/uploading a file and playing internet radio using mplayer are my usual use cases, but I often have different tasks that have to be performed in a specific time window. Both rsync and mplayer stop when there's connection break, so I want them to be restarted. That's all. –  liori May 8 '10 at 13:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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