Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possibly related to this previous question, I would like to be able to run a task for a specified amount of time, then stop/kill it, then start it again.

Specifically, I run the Folding@Home application on my Linux workstation. Periodically, it will have trouble logging-in to the home server, and will sit idle for a long time, but not release memory/resources it has claimed (yes, I realize this is likely a bug in the tool, but I can't fix that currently). Typically just the act of killing and starting the tool will allow it to check-in again.

So, I would like to be able to run something like:

run_for <time> ./fah6

Does such a tool exist, or should I write a wrapper than nukes the fah6 client after, say, a week and restart it?

share|improve this question
up vote 5 down vote accepted

I may be old school, but this sounds like a job for cron. Just kill / restart it with cron on some appropriate schedule. You'll have to take into account if fah6 can "pick up where it left off" easily, but I don't see any problem with a daily restart.


share|improve this answer
it does pick up where it left off .. and I had not considered using cron for this one :) .. I'll see how hard that may be to do – warren Jul 15 '10 at 13:43
I like this idea. You could use two cron jobs to help ensure that it runs only during non-work hours -- one that does killall fah6 at 9:00 am and another that starts it up again at 6:00 pm. – Doug Harris Jul 15 '10 at 14:06
this has turned out to be a very usable suggestion. Thanks! – warren Jul 27 '10 at 16:01

timeout tool.

timeout - run a command with a time limit

timeout [OPTION]

Start COMMAND, and kill it if still running after DURATION.

for example:

    timeout 15s ./fah6 
share|improve this answer
sleep 60 & VAR=$! ; sleep 10 ; kill $VAR

In this example, 'sleep 60' is a placeholder for the command you want to terminate early. Replace '10' with how long you want to run your timed command (at most) in seconds. The above should exit after 10 seconds, killing the 'sleep 60' process.

Note that this is for bash.

share|improve this answer
Thanx! This simple solution ultimately does the job! – nightcoder Dec 22 '12 at 9:36

cron will work, but if you only wanted it run once, or if the time will change from day to day then it would be simpler to use at (at man page):

$ ./fah6 &
[1] 12345
$ at 1800
at> kill 12345
at> <EOT>

The steps are:

  • start fah6 as a background job (the & at the end of the line starts it as a background job). The next line reports the process number
  • at is followed by the time to end the job
  • the at> prompt appears, type kill and the job number reported above
  • to stop entering commands, quit by pressing Ctrl-D
share|improve this answer
how would I know the process number if I want to do this in an unattended fashion? – warren Jul 16 '10 at 12:32
@warren, you can use $!, as in slartibartfast's answer – Hamish Downer Jul 16 '10 at 13:57

Personnaly, I would implement this within the script the capability of running, waiting and killing the subprocess.

In python with something like:

If it's not in Pyton, use os.system('./fah6') in the code of the script that handle wait/stop/restart

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .