Chapter 11 – Schedule Tasks

We have learned a lot since we started this tutorial. We are starting to gain more confidence in operating a Linux system.

However, so far, we have been limited to running manual commands. In other words, we have to manually type in commands and press enter in order to execute a task. Having to rely solely on this manual task execution can limit our ability to administer a Linux system. Often, we would need to schedule certain tasks (or jobs, as we call them) automatically at a predetermined time or interval without having to run them manually.

This chapter will introduce you to two Linux command-line utilities that will allow you to do just that.

Schedule Tasks to Run Once

The first command we are going to cover in this chapter is at.

This is a Linux command-line utility that allows you to schedule certain tasks to run only once at a certain time and date.

Installing the command

To start using at, you should first run it with no arguments to confirm that it is already installed in your Linux.

If it is not the case, then you can simply install it using your distribution’s packet manager. For instance, if you’re running Linux Mint, Ubuntu, or any Debian-based distro, then simply run the following command:

sudo apt-get install at

Schedule a Task

The syntax for running at is as follow:

at [OPTIONS] runtime

If you want to schedule a job to run at a certain time, then you can specify the time as an argument to at as the above syntax shows.

You can specify time in the form of HH:MM or using the am/pm suffixes. You can also specify the date in the form of MMDDYY.

Then, once you press Enter and the command is run, you should find yourself on a command prompt that starts with at>. You can then start typing the commands that will be part of the job that you want to schedule. When you’re done typing all the commands, just press Ctrl-D and that should bring you back to your normal command prompt.

schedule tasks using at

You can use at with -l option to list all your current jobs, and you can also remove a job using the -r option followed by the number of the job to remove.

list and remove scheduled jobs with at

Schedule Tasks to Run Periodically

Cron is a Linux utility that will allow you to run tasks on a predetermined schedule, instead of always having to run every task manually in real-time.

Backup Case

Let’s say that you want to backup your website every day at 3 am. One way to do this is to wake up every night at 3 and manually run the backup command, which might be something like this :

$ cp /website/ /websitebackup/

Well, you can see how cumbersome that can be. Thankfully, this is where the Cron Jobs come in. They allow you to run a task on a schedule. For our case, all we have to do is to create a Cron job that will run the above command every day at 3 am.

Cron Table

Now that we know that the cron utility is what allows us to schedule tasks, let’s try and create a Cron Job.

You can view and add Cron jobs in configuration files called crontabs (short for Cron Tables). There is one particular crontab that is general and that you can use to schedule system-wide jobs. This special file is located at /etc/crontab and follows a standard structure.

Every line in this file defines a job. Here is the structure of a job definition :

crontab job definition

As the image above shows, there are seven total columns. The first five represents the date and time when to run the job. The fifth column is the user that the task will be run as. Finally, the last column represents the command that will run in this job.

Time and Date

In order to define the time and date, here is something to keep in mind.

When the asterisk symbol (*) is given for a column, then that column will match all its possible values. For example, if we keep an asterisk (*) in the hour field, this means that every hour is a match.

If, however, we give the hours column the value of 7, then it will only match 7 am.

Back to our Case

Before we move on, let’s go back to our example.

Here is the job definition that will run our backup command every day at 3 am.

* 3 * * * user cp /website/ /websitebackup/

You should now be able to understand the above line on your own. If you are having difficulties, you can re-read this chapter before you can move on.

It is important to fully understand all that we have covered so far in this tutorial. In the next chapters, we will be discussing bash scripting, which is different from what we have covered so far, and a bit more challenging. So, if you feel that there is a chapter that you didn’t fully grasp, I invite you to go back and re-read it. This way, we can all be on the same page when we embark on the next phase


1 thought on “Chapter 11 – Schedule Tasks

Leave a Reply

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