Schedule Linux Task with Crontab

While working in Linux, if you ever get to work on a website that is quite heavy on end of your web server, then at that time you may also want to run some of the processes like generating the thumbnails and improve the data at the back end, allowing it to stop interfering with the user interfere. To facilitate its user, Linux also has a wonderful program that could be used the name as cron. Cron helps us to run the task automatically in the backend in a particular sequence of regular intervals. Cron has many other uses like it creates backups automatically, it is also used for synchronization of files, it can be used to update the schedules etc. This tutorial will be using initially command line of Linux to see how it works and in the later section, we will see GUI for making Cronjobs.

Linux Crontab

Crontab is basically used in order to see the commands and to schedule the commands further so that they can be executed periodically. For the usage of this command, we need to follow following steps.

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

Search for Linux Terminal

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

Open the Terminal application

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

Linux terminal is ready

Step 4:

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

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 following command. “sudo apt-get update && sudo apt–get install gnome-schedule”. After that it will ask 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 the 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 may be 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 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 the tasks using crontab. The first part of the tutorial is based on the methods applicable on the command line. Whereas, the second part that seems much easier to be used is for the GUI based Cron jobs that can be made to work by installing the Gnome schedule as shown in this tutorial.

 

Scheduling a Task on Linux Using Crontab