Running script using crontab on Ubuntu

When playing around with my raspberry pi I sometimes want to automate scripts and make them run on timed intervals. This is a perfect job for crontab, so I will try to give a short explanation of how I use it here.

In a terminal window, run: crontab -e where -e is for edit. If no crontab exist, it will create one. If you haven’t used crontab before, you will be prompted with this:

Crontab editor prompt
Crontab editor prompt

If you are unfamiliar with these, I suggest using nano (choice 2). I recommend learning vim.
When you have chosen your editor, a file is opened in your chosen editor. I will assume you have chosen nano. It should look somewhat like this:

crontab file opened in nano
crontab file opened in nano

If you have a look at the last line in that file, that is the command running the script.
The first statement tells it to run every 15 minutes. The 4 trailing stars are for other timeattributes.

The order of the five stars is as follows:

  1. Minutes (0-59)
  2. Hour (0-23)
  3. Day of month (1-31)
  4. Month (1-12)
  5. Day of week (0-6, Sunday = 0)

The next part is the command to run. I’ve used full paths, but it is not necessary.
When you have added your job, you can save using Ctrl+O, then exit using Ctrl+X. Your crontab should now run in the interval you have specified.

You can read more about it here: http://www.adminschoice.com/crontab-quick-reference

And that’s all! Tell me what you think in the comment section below!

Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “Running script using crontab on Ubuntu”