facebook youtube pinterest twitter reddit whatsapp instagram

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

Output:

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!

Related Post(s)

  • Monitoring Multiple Log Files In RealTime With MultiTail (Ubuntu)

    Oh my... I really find scanning through the logs file time consuming, and painful. Luckily for me, I founded Multitail, which is an awesome, and powerful tool for not only browsing through several f

  • How To Find Files Using Locate in Ubuntu

    It's kind of frustrating when you are searching for a particular file, and you have no idea of how to find it, In this guide, I'll walk you through on two different ways you can find a file in your G

  • Managing MariaDB Databases (Ubuntu Server)

    In this guide, you'll learn how to manage MariaDB databases in your terminal, from connecting to the database server using the mariadb command, creating a database, removing (drop) database, and mana

  • Understanding the Linux Filesystem

    Understanding the filesystem is important if you want to know how data is stored and retrieved, but really, the term might be somewhat confusing in Linux, as it means two different things. A filesys

  • Viewing Disk Usage in Ubuntu

    There are several ways to view disk usage in your Linux system, and that is what we would be going over in this guide... Out of the box, Linux provides us the df command, which is the standard Unix c

  • Understanding & Using Symbolic & Hard Links (Ubuntu)

    Have you ever wondered how you can create shortcuts that link to other files in Linux? Well, let me introduce you to symbolic and hard links. The two types of links in Linux are symbolic links and ha