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

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.


You're looking for cron and shell scripts.


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

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

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.


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

Look into incron