4

Good day!

I'm working from Chile. The thing is the connection here is super slow and tasks like have every station updating its packages directly to internet it's just not possible. So I have a local repository which I'd like to update via rsync only at nights.

I don't have a problem with rsync, I know how to use it. Where I'm a bit new is in areas like cronjobs and similar stuffs.

Long story short, what I need is to do is update my repository every night. Let's say it starts every day at 7:00 PM and stops at 7:00 AM.

How can I do that?

Thanks in advance.

Warm regards.

PD: I use Ubuntu 16.04.

1 Answer 1

8

There are two ways to use cron, by creating a file in /etc/cron.d/repo-sync with the format:

<minute> <hour> <day_of_month> <month> <day_of_week> <user> <command>

For example, to run rsync from src to dest at 19:00 every day you can use:

0 19 * * * root rsync -a src dest

Or you can create a crontab by running crontab -e (as the user you want to run the command as) with a line like above minus the user such as:

0 19 * * * rsync -a src dest

Personally I prefer creating cron.d files for system/root tasks but both approaches will have an identical result.

Now this will run the command once at the given time - it will stop once the command has finished running so there is no real need to specify a command to stop it at 7:00 as rsync should have hopefully completed by then. If you truly must stop the command at 7:00 you can issue another cron job that simply kills existing processes at the given time.

0 7 * * * root killall rsync

Unfortunately this will kill all rsync commands - which might have side effects if anything else if trying to run rsync at the same time so I do not advise this method.

You can go further by putting the rsync command inside a script that writes its pid to a file, and use that pid in the kill command to mitigate this but this might be more complicated then you really need.

You can read more about setting up cron jobs here or here.

1
  • 3
    There's also timeout, which runs arbitrary commands and kills them after a timeout, like /usr/bin/timeout 12h /usr/bin/rsync … Sep 17, 2017 at 16:34

You must log in to answer this question.

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