Understanding & Scheduling Tasks With Cron [Ubuntu]

I previously wrote a tutorial about managing system processes in ubuntu, where we walked through starting, stopping programs, and the likes.

In some cases, you may need an application to perform a task at a specific time rather than always running in the background, this is where cron comes in. By using Cron, you can schedule a process, script, or program to run at a specific time.

Let’s get started…

Each user can create a cron job, which is under a crontab, a crontab includes a list of cron jobs(one per line), also, the root user also has crontabs for system-wide administrative task.

Viewing a Crontab of a user

crontab -l

This will show you a list of jobs for the use that executed the above command, if you executes ut as root (sudo), you’ll see root crontabs.

Viewing a Crontab of a Specific User

If you want to view a crontab for a specific user, you can use the -u option e.g

sudo crontab -u James -l

You would see the message no crontab for user¬†if you haven’t created a cron job.

Creating a Cron job

To create a cron job, log in to the user you want the task to run under, then execute the following command:

crontab -e


user@server:~$ crontab -e
no crontab for user - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]:

If you have more than one text editor, you may see the above output, I’ll go with nano, so, I’ll enter 1, you can change it later by running ‘select-editor’ or by simply running Editor=vim crontab -e, which would take you directly to a text editor.

Once you enter 1, hit enter, and a default crontab file would be opened with some useful info about how cron works, to add a new job, add it at the bottom of the file, each task to run has to be defined through a single line indicating with different fields when the task will be run and what command to run for the task, e.g:

m h dom mon dow command

Each cron job has six fields, each separated by at least one space or tab spaces:

  • The first field shows the minute (m) in which the job would occur,
  • The second field shows the hour (h) in the 24-hour format (0-23) the job would occur,
  • The third field represents the day of the month (dom), if you place 7, that would indicate the 7 of the month
  • The fourth field represent the month (mon), if you place 7, that would correspond to july
  • The fifth field is the day of the week (dow), starting from 0-6, 0 represent Sunday – 6 (Saturday)
  • The last field is the command to be executed

Updating Server Repository Index at 7 Pm Every Friday

* 19 * * 4 /usr/bin/apt-get update

The * asterisk means anytime, so, the above command simply updates the server repo every Friday (4), at 7 pm. You would notice I used the full command path, this is considered best practice. If you don’t know what the full command is, you can use the which command.

There are a whole host of other command combos you can try, enjoy!

Comment policy: Respectful and beneficial comments are welcome with full open hands. However, all comments are manually moderated and those that doesn't relate with what the passage is saying or offensive comments would be deleted. Thanks for understanding!

Leave a Reply

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