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 want to run an rsync command at midnight but make it stop at 8am. How can i schedule the start AND END of a command?

I've looked into cronjobs but that will only start at a certain time. Any ideas on how to end this process? Ill be running it from OS X. I thought about logging in as a new ssh user and just using a cronjob to start a bash script to kill that connection but that seems like overwork.

share|improve this question

migrated from stackoverflow.com Mar 13 '13 at 4:52

This question came from our site for professional and enthusiast programmers.

    
Have you tried using ulimit -t to cap the amount of time it can execute? –  Randy Howard Mar 12 '13 at 22:49
    
@RandyHoward: ulimit -t will limit the CPU time used, but rsync generally uses very little CPU, being limited by I/O. –  Zan Lynx Mar 12 '13 at 23:28
add comment

2 Answers

You could have a shell script write the rsync PID into a file at a fixed location. I would get the PID by launching rsync in the background (append &) and use $!.

rsync blah/ remote:blah/ &
echo $! > pidfile

Then another cron job at 8 am would read that file and put the result into RSPID.

RSPID=`cat pidfile`

Then run ps $RSPID | grep rsync to see if it is still running, then do kill $RSPID.

Something along those lines should work.

You could also put the kill command into the shell script. Launch rsync in the background and sleep 8 hours, then kill the rsync.

share|improve this answer
    
+1 Simple, fun and tasty –  uʍop ǝpısdn Mar 13 '13 at 0:18
add comment

Add a second crontab entry that kills rsync at 8 am. Another option would be to start rsync in the background and have the script loop until 8 am before it kills the process:

#!/bin/bash

rsync ... &
pid=$!

while /bin/true; do
  if [ $(date +%H) -ge 8 ]; then
    kill -TERM $pid
    exit 0
  else
    sleep 60
  fi
done
share|improve this answer
add comment

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.