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

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 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 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:


rsync ... &

while /bin/true; do
  if [ $(date +%H) -ge 8 ]; then
    kill -TERM $pid
    exit 0
    sleep 60
share|improve this answer
Could you give me the official URL to the explanation of '$!'? I couldn't google it coz it's somewhat recognized as special characters by Google. – Judking Apr 17 '15 at 1:44
@Judking It's documented in the section PARAMETERS of the bash man page (under "Special Parameters"). It's also explained in chapter 9.1 of the Advanced Bash Scripting Guide (under "Other Special Parameters"). – Ansgar Wiechers Apr 21 '15 at 11:57

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

You must log in to answer this question.

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