2

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?

5 Answers 5

5

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.

Hotei

3
  • 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, 2010 at 13:43
  • 1
    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. Jul 15, 2010 at 14:06
  • this has turned out to be a very usable suggestion. Thanks!
    – warren
    Jul 27, 2010 at 16:01
9

timeout tool.

timeout - run a command with a time limit

timeout [OPTION] DURATION COMMAND [ARG]...
timeout [OPTION]

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

for example:

    timeout 15s ./fah6 
3
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.

1
  • Thanx! This simple solution ultimately does the job!
    – nightcoder
    Dec 22, 2012 at 9:36
1

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
2
  • how would I know the process number if I want to do this in an unattended fashion?
    – warren
    Jul 16, 2010 at 12:32
  • 1
    @warren, you can use $!, as in slartibartfast's answer Jul 16, 2010 at 13:57
1

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

In python with something like:

http://docs.python.org/dev/library/multiprocessing.html

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

You must log in to answer this question.

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