Schedule Linux Task with Crontab

Cron helps us to run tasks automatically in the background in defined intervals. Cron is e.g. used to automatically create backups every night to sync files e.g. once an hour or to start updates or download files at specific intervals. This tutorial will show you how to set up and edit cronjobs using the crontab command and the GUI tool Gnome Schedule.

Linux Crontab

Crontab command is used to list and edit cron jobs. For the usage of this command, we need to follow the following steps.

First of all, open Terminal by clicking on Ubuntu launcher and search for Terminal.

Search for Linux Terminal

Now click on the Terminal and wait for the terminal to open.

Open the Terminal application

Once the terminal is opened, you will have a screen like this:

Linux terminal is ready

List existing Cron Jobs

In order to check which crontabs are running presently on our system, we will use the command “sudo crontab –l”.

Show crontab content

Enter the required credentials.

Enter your password

As you can see we are having no crontabs for this directory because there has been no crontab created for this root user.

No root crontab set yet

Add or Edit Cron Jobs

To open the crontab in our default editor we use the command, crontab-e.

Edit the crontab

Enter the required credentials.

Enter your password

If you are using crontab for the first time ever, then you are made to select one editor.

Choose a crontab editor

You may select any one of your desire. The tasks running in the background will appear.

Crontab ready for editing

If you are using it for the first time, then select the Nano editor. You’ll be able to find out the Nano text editor, that is identified by the “GNU nano” header located at the top of your window of the terminal. In case, if you don’t, crontab probably will be opened in the vi text editor.

Crontab opened in vi text editor

And if you’re not much comfortable in using vi, you easily quit into vi. After pressing enter you will be able to close it.

Quit

Adding a new cron task

We can use the arrow keys/ page down keys to scroll to the bottom of the crontab file in Nano. All of the lines that begin with # are “comment lines”. These comments are helpful for the people who are editing the files by providing essential information of their use.

Lines that are written in the crontab file are in the following sequence, they have following acceptable values:

1) minute (0-59)

2) hour (0-23)

3) day (1-31)

4) month (1-12)

5) weekday (0-6)

6) command

Add a new cronjob

We have used * character to match any of the value. Now if we want to the command /usr/bin/example every day at a particular pre-defined time say 12:30 then this is what will we do. We will use 29 0 * * * /usr/bin/example. We have a zero here because an hour starts with a zero but day begins with a 1.

So, this is how we schedule a particular task.

Saving the crontab

We use ctrl+o in order to save the file in crontab in Nano.

Save the crontab

Scheduling Cron jobs Using GUI

Step 1:

For this, you simply go to the command line and type the following command.

sudo apt-get update && sudo apt-get install gnome-schedule

After that it will ask for the credentials once we enter them, the GNOME schedule will begin to install. This method is much easier since we just have to enter the required fields. So, once it is installed you may see how helpful it is for you to schedule the cronjobs using Gnome scheduling.

Install Gnome Schedule

It will ask permission so press Y to continue.

Gnome schedule app is being installed

Step 2:

After a while, the GNOME schedule has been installed. It will appear as scheduled tasks in the applications of the system.

Search for Gnome Schedule application

Step 3:

On double-clicking, we will get a window that is “Configure Scheduled Tasks”.

Configure scheduled tasks

Step 4:

We see the “New” field in the above screenshot. Click on it and then from the drop-down menu select Recurrent Task.

Create a scheduled task

In the above window, we need to fill out all of the asked information which is mandatory to create a new job. This information is going to be:

  1. Description of the job (or the name for the job)
  2. Command (could be any we want to run it as the recurring task)
  3. Behavior (If we desire to restrain the output command)
  4. Basic (this includes the information about the task whether it will run every minute or an hour, or per day, per week, or every month)
  5. Advanced (this includes if we want to schedule the job for a pre-defined specific time).

On looking at this window’s bottom, a bottom saying “Add as Template” present. means we can add a job as a template. If this a job, then we surely can base other jobs on it. On creating the template jobs, we can eventually create numerous jobs based on that template. For this, we just need to click on “new- drop down” as we did previously and then we click selecting from the template.

If we want to create the cron jobs, sudo permissions are also needed to run. So, we open up a terminal window and then Issue the command sudo gnome-schedule. We then schedule the job as described above.

Conclusion

In this tutorial, we have discussed how to schedule tasks using crontab. The first part of the tutorial is based on the command line. Whereas, the second part shows how to maintain cronjobs using GNOME GUI.

Scheduling a Task on Linux Using Crontab
Avatar

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn