Write up: How to schedule tasks the right way in Linux, using crontab

by | Apr 23, 2022


Premium Content

 

Patreon

 

 

 


Subscribe to
Patreon to watch this episode.


 

Watch a crontab related episode below:

 

Reading Time: 6 Minutes

 

Introduction

Cron is named after the Greek word “Chronos” which translates to “time”.

Cron is a time-based scheduling utility that allows you to schedule various jobs to run periodically or even on a system reboot. It is really helpful because you can run recurring tasks overnight by just scheduling them periodically or even run commands/scripts automatically in the background when your system boots. It comes with most Linux distributions, but for this tutorial we will use Kali Linux.

 

Creating Cron jobs using Crontab

The cron utility runs based on commands specified in a cron table, crontab. Crontab is a daemon that continuously run and executes specified jobs/commands. Each user, including root, can have a cron file. The file doesn’t exist by default, but it can be created using the crontab-e command, also used to edit a cron file, and upon executing the command, it will create the file under the /var/spool/cron directory.

 

The crontab command uses Vi as it’s default editor since Vi is always present even on the most basic installations, but in most cases, it will prompt you to select an editor of your choice.

crontab -e

 

SS1

 

We will use nano as our editor for this tutorial, you can choose a different editor at any time by using the select-editor command.

By selecting the second option it will automatically open the file with the nano text editor. New cron files do not include any jobs, so commands must be added from scratch. Each file contains examples and provides information on how to schedule jobs with the correct format.

 

SS2

 

See Also: Write up: Hacking is an art, and so is subdomain enumeration.

 

Crontab Syntax

The crontab of Linux contains six fields. The first five fields can be used to define the time and date of execution while the sixth is used for the command execution.

 

Field NameDescriptionAccepted Values
mminute field0 to 59
hhour field0 to 23
domday of the month field1-31
monmonth field1-12
dowthe day of week field0-6
commandthe script or command to be executedcommand or script

 

Format example

See Also: Write up: Steganography: Hide data in images and extract them

 

Scheduling Cron Jobs

 

Scheduling a job for a specific time

This is one of the basic usages of cron, to execute a job in a specific time. The command below will execute the command to clear the bin on 10th August 19:00. The time field uses 24 hours format. Meaning that for 10AM you should use 10, and for 10PM use 22.

First make sure that the cron utility is running on your device with the command service cron status, if not, start it by using the command service cron start.

Now add the command below (enter your username into the <user> field and your date of preference).

*Depending on your Linux distribution your command for removing the files from the Trash could be different from what is shown below.

00 19 10 08 * rm -rf /home/<user>/.local/share/Trash/files

00 – 0th minute
19 – 07 PM
10 – 10th day
08 – 8th month, August
* – Every day of the week – (The * means all the possible units)

 

To view crontab entries use crontab -l

 

SS4

 

Scheduling a job for an x amount of minutes

It might not be practical having to schedule a job for every minute, but understanding this will help understand other examples as well.

* * * * * <command>

The * means all possible unit. In the example above, It translates to every minute of every hour throughout the year.

You can tweak the example above to schedule a job for every 20 minutes just by adding /20 next to the * of the minute field.

*/20 * * * * <command>

 

 

Scheduling a job to run for multiple times every day

You may want to schedule your device to check and run updates twice a day using the cron utility. The example below will execute the update command at 02:00 and 14:00 every day.

00 02,14 * * * apt-get update

Using the same logic, you can add also other times of the day to schedule your command or script by using the comma on the hour field.

 

 

Scheduling a job to run for weekly tasks

The example below will execute an update command every Monday at 12PM.

00 12 * * mon apt-get update

 

Scheduling a job to run for a specific month or months

The example below will execute a task to run in January, June, October update command every Monday at 12PM.

* * * jan,jun,oct * command

 

Scheduling a job to run for a certain range of time.

The example below will execute a task on every day during the hours 18:00 – 06:00

00 18,06 * * * command

00 – 0th Minute
18,06 – 18pm,19pm,20pm,21pm,22pm,23pm,00am,01am,02am,03am,04am,05am,06am
* – Every day
* Every month
* Every day of the week

 

Scheduling a job to run for a certain range of time at weekends.

The example below will execute a task on Weekends during the hours 18:00 – 06:00

00 18,06 * * 6-7 command

00 – 0th Minute
18,06 – 18pm,19pm,20pm,21pm,22pm,23pm,00am,01am,02am,03am,04am,05am,06am
* – Every day
* – Every month
6-7 – Saturday, Sunday (1-5 = Mon, Tue, Wed, Thu, Fri)

 

Scheduling a job to run on system reboot – using keywords.

Crontab includes special keywords for easier scheduling of tasks.

 

KeywordMeaning
@hourly0 * * * *
@daily0 0 * * *
@yearly0 0 1 1 *
@rebootRun at startup.

 

The example below will execute a task to run on system update reboot.

@reboot command

Using the same logic we can run commands hourly, daily, and yearly by just entering the correct keyword from the table above. By using the @yearly keyword, it will execute the task on the very first minute of every year.

 

See Also: Write up: Detect malicious hacker activities on endpoints

 

 

Last Words

Cron can be very useful on many occasions, either to back up your system, run checks about the status of your database, update your machine or any other task you want to be done repeatedly and without the need to do it manually. It comes free with every Linux distribution and it can be a powerful tool for every type of demand you may need it.

 

 


We hope that this write up has taught you something new. If you enjoyed it, the best way that you can support us is to share it! If you’d like to hear more about us, you can find us on LinkedInTwitterYouTube.


 

Merch

Share This