Is there a way to have Linux run a command every X minutes?

8

I'm using RSync with my buddy just for testing and general geekery, we want to somehow schedule the sync task to run every X time.

Using the terminal, we run the command and it works.

  1. What can we use to run a script every X time?

  2. How can we program something like a Windows .bat file for Linux?

Our operating system is Ubuntu.

Sergio Tapia

Posted 2010-10-22T00:43:23.470

Reputation:

Answers

21

You're looking for cron and shell scripts.

Ignacio Vazquez-Abrams

Posted 2010-10-22T00:43:23.470

Reputation: 100 516

2@Bobby: I wasn't giving Google as an answer. But thanks for playing. – Ignacio Vazquez-Abrams – 2010-10-22T18:13:00.277

12

following would run a script every 3 minutes if placed in your crontab

*/3 * * * * /home/sergio/myscript.sh 

Tim Hoolihan

Posted 2010-10-22T00:43:23.470

Reputation: 223

1I have never used the slash notation. That is nice. +1 for you! – Buggabill – 2010-10-22T14:38:21.257

4

For part A, you'll want Cron. Tim Hoolihan has a good example of that in his answer.

For part B, you'll want a shell script. To make one, just make a textfile that starts with the following line:

#!/bin/bash

And then follow that with commands like you were typing into the shell. (Advanced tip: the #! syntax works for any command-line program, not just bash.)

Once that's done, save it (it's recommended to use a .sh extension, but not at all neccessary), go to your shell and run chmod ugo+x filename.sh, substituting the actual filename, of course. This will make it so your script can be executed.

Finally, just put the script in the crontab per Tim's answer.

Hope this helps.

MiffTheFox

Posted 2010-10-22T00:43:23.470

Reputation: 3 032

3

In the context of rsyncing files, you can also make it happen as soon as there are new changes.

Look into incron

vtest

Posted 2010-10-22T00:43:23.470

Reputation: 4 424

Just fyi, incron doesn't look like it's been updated out of alpha status since this answer. – Josephine Moeller – 2015-07-16T21:33:31.040