linux shell, keep task alive for specified time

4

2

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?

liori

Posted 2010-05-07T20:49:41.043

Reputation: 3 044

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.

Paused until further notice.

Posted 2010-05-07T20:49:41.043

Reputation: 86 075

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 – 2010-05-07T22:09:25.590

0

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.

mpez0

Posted 2010-05-07T20:49:41.043

Reputation: 2 578

1If I wanted to write a script, I'd ask on stackoverflow. Here I am just looking for a ready solution. – liori – 2010-05-08T13:42:29.117

-1

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.

msw

Posted 2010-05-07T20:49:41.043

Reputation: 3 287

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 – 2010-05-08T13:41:40.583