Task scheduling on Linux: CRONTAB

CRON is a task scheduler (a daemon) which runs commands at predetermined times and intervals. You can choose the exact time at which the command will run by setting the minute, the hour, the day and the month. You can also define the periodicity, e.g. a command can be configured to run every ten minutes, every fourth hour, or at a particular time on Sunday night.

A cron job can be used to send emails, to backup data, to update with the latest security patches, etc..

When the system boots, the cron daemon starts and remains operational as long as the system is up and running. There are configuration files that contain a set of commands or command lines along with their execution times. Cron will read these files and invoke the commands at their scheduled times. These configuration files are called crontabs.

If you want to display the crontabs that are currently running , issue the command below:

sudo crontab -l

image8.png

As you can see there are no crontabs for the user root. In fact, each user has its own crontab file which is contained in the directory /var/spool/cron. The names of the crontab files contain the login names of their corresponding users. This will allow cron to tell which UID to use when invoking the list of commands contained in each configuration file. In order to edit the crontab of another user, you can use the following arguments for the crontab command: crontab -u user_name -e

In order to see the list of the commands that were executed and their scheduled times , you would need to check the directories /var/cron/log (in Red Hat) or /var/adm/cron/log since cron keeps its log file in these folders (in /var/spool/cron/crontabs in Debian and Ubuntu).

Note that crontab files can be defined system-wide or by a single user. For system-wide files , you would need to check the directories /etc/crontab and also the files in /etc/cron.d :

image5.png

As the root user, you can run the crontab -e command in order to schedule software updates.

image4.png

You can choose your preferred editor (vi command by default) as shown above in order to edit a crontab file.

image9.png

Here’s an example command to be interpreted by cron:

# min hour day/month month day/week command

59 10 * * 1 apt-get update -y | mail net2_admin@localhost

The first five fields designate day and time whereas the last field tells cron when to run the command. The command can be any valid shell command without quotes and may contain tabs or spaces..

In the example above, the first line starts with a pound sign (#) which signals to cron that this is just a comment line.We can see here the name of the fields for illustration purpose.

In the second line, the command [ apt-get update -y | mail net2_admin@localhost ] is run at 59 minutes after hour 10 (10:59 a.m). The star (*) matches all values This then will instruct cron to run the command every monday of each month.

The following example will tell cron to run updates at 09:44 a.m Monday through Thursday:

44 09 * * 1-4 yum -y update

image7.png

Two integers therefore would indicate a range of values. If the first field contains a comma for instance : 0.15 that would indicate that cron will run every 15 minutes.

The comma specifies a set of values for repetition. For example, if write 1,2,5 in the Hour slot, the corresponding task will run at 1 am, 2 am and 5 am respectively.

image6.png

15 20 * * 0-1,4 rm -rf ~/.cache/thumbnails/*

The command above will tell cron to remove the thumbnails every Sunday, Monday and Thursday ( Thumbnails help your computer execute certain operations faster. They are similar to temporary files. Overtime they increase and will become a burden ).

If however you want to specify periodicity, for example you want to instruct cron to run a command at the following hours of the day 0,4,8,12,16,20, you can use the / operator: it helps you specify the period, .e.g. if you write */4 in the Hour field , that would mean that the task will be carried out every four hours.

Keywords shortcuts or predefined macros

You have the possibility to also specify some predefined special keywords shortcuts instead of the numerical values (i.e. hourly, daily, etc.)

  • @yearly (or @annually) – Runs the specified command once a year at midnight on the 1st of January. This is equivalent to 0 0 1 1 *.
  • @monthly – Runs the specified command once a month, on the first day of the month and at midnight. Equivalent to 0 0 1 * *.
  • @weekly – Runs the specified command once a week on Sunday and at midnight. Equivalent to 0 0 * * 0.
  • @daily – Runs the specified command daily at midnight. Equivalent to 0 0 * * *.
  • @hourly – Runs the specified command every hour at the beginning of the hour. Equivalent to 0 * * * *.
  • @reboot – Runs the specified task at startup.

Crontab commands and management

You can install or edit a crontab file via crontab command ( replacing any previous version):

crontab filename

Here is a list of commands with the options that will allow you to modify, add or remove cron jobs :

crontab -l

This will display the content of your crontab (sent to standard output)

crontab -r

This will remove your crontab file (therefore you no longer have a crontab file).

If for instance you write

crontab -r mradmin

This will erase the crontab of the user mradmin. You can have both a filename and a username arguments in the same command (e.g., crontab -u mradmin crontab.new)

crontab -e

This will edit your crontab file and then resubmits it to the crontab directory.

NOTE: If the system is down, cron does not re-run the commands that are missed in the meantime.

For more information on the format of a crontab file, type man 5 crontab. To read about the crontab command, type man 1 crontab.

Crontab submission permission settings

The two config files, cron.allow and cron.deny allow system administrators to specify which user has access to the crontab command ( or which user may submit crontab files).

If the cron.allow file exists, then you will find a list of all users entitled to submit crontabs. If a person is not listed, then he is not permitted to invoke the crontab command.

If you add a username to the cron.deny file, he will be denied access to the crontab commands.

On most systems, it is important to mention that access control is carried out by crontab and not by cron. If for instance a user unintentionally puts a crontab file into the appropriate directory then cron will nevertheless execute the commands therein.

Cron jobs scheduling via GUI

It is possible to schedule cron jobs via a graphical user interface. To do so , open up the terminal

and issue the command :

sudo apt–get install gnome-schedule

Make sure that your packages are up to date beforehand by running sudo apt-get update.

Once the installation is complete, look up the application you have just installed , i.e. scheduled tasks and double-click on it.

image1.png

You will then get the screen below entitled ‘Configure Scheduled Tasks’ :

configure scheduled new tasks
Clicking on New will bring up the window below :

image3.png

You just need to fill out all of the required information in order to create a new job:

  1. Job description
  2. Command
  3. Behavior
  4. Basic (periodicity)
  5. Advanced (specific date and time).

2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
adminRogelio AH Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Rogelio AH
Guest
Rogelio AH

I think , it is wrong this instruccion o interpretation
15 20 * * 0-1,4 rm -rf ~/.cache/thumbnails/*
It runs on Sunday, monday and thursday

Close Menu
Show Buttons
Hide Buttons