Cron jobs

Cron jobs are jobs or tasks that run at regular intervals of time unlike the at command. For example, in office, my job is to keep all the detailed information of company employees that is confidential. To keep it secure and updated without any loss of information, I will have to take the backup of the latest data in external devices such as a hard disk or a flash drive. Depending upon the number of employees, I may have to take the backup on a minute, hour, daily or weekly basis. It's hard, tedious, and a waste of time to back up manually every time. By having the knowledge of how to schedule a cron job, it can be very easily achieved. A Cron job creation is frequently done by system administrators to schedule tasks that are to be performed at regular intervals, for example, taking the backup of a system, saving logs of each user who is logged in, monitoring and reporting the network usage of each user, performing system clean-up, scheduling system update, and so on.

Cron consists of two parts: cron daemon and cron configuration.

Cron daemon

The cron daemon automatically starts when a system is booted and keeps running in the background. Daemon process is known as crond and is started by systemd or the init process, depending upon what your system has. Its task is to check configuration files regularly at one minute intervals and check whether any tasks are to be completed.

Cron configuration

Cron configuration contains files and directories where the Cron jobs to be scheduled are written. They are available in the /etc/ directory. The most important file associated with cron configuration is crontab. In a Linux system, configuration files related to cron are as follows:

  • /etc/cron.hourly/: This contains the scripts to be run each hour
  • /etc/cron.daily/: This contains the scripts to be run once in a day
  • /etc/cron.weekly/: This contains the scripts to be run once in a week
  • /etc/cron.monthly/: This contains the scripts to be run once in a month
  • /etc/crontab: This contains commands and the interval at which they should run
  • /etc/cron.d/: This is the directory with files having commands and the interval at which they should run

Scripts can be directly added into any of the directories such as cron.hourly/, cron.daily/, cron.weekly/, or cron.monthly/, in order to run them at an hourly, daily, weekly, or monthly basis respectively.

The following is a simple shell script firefox_memcheck.sh, which checks whether a Firefox process is running or not. If Firefox is running and its memory usage is greater than 30 percent, then restart Firefox:

#!/bin/sh
# Filename: firefox_memcheck.sh
# Desription: Resatrts application firefix if memory usage is more than 30%

pid='pidof firefox' # Get pid of firefox
if [ $pid -gt 1 ]
then
  # Get current memory usage of firefox
  current_mem_usage='ps -u --pid $pid| tail -n1 | tr -s ' ' | cut -d ' ' -f 4'
  # Check if firefox memory  usage is more than 30% or not
  if [ $(echo "$current_mem_usage > 30" | bc) -eq 1 ]
  then
    kill $pid   # Kill firefox if memory usage is > 30%
    firefox &   # Launch firefox
  fi
fi

We can add this script into the /etc/cron.hourly/ directory of the system and it will keep checking our Firefox memory usage. This script can be modified to monitor the memory usage for other processes too.

Crontab entries

By putting scripts into cron.{hourly, daily, weekly, monthly}, we can only set tasks at an interval of an hour, day, week, and month. What if a task has to run at 2-day intervals, 10-day intervals, 90 minute intervals, and so on? To achieve this, we can add tasks into the /etc/crontab file or the /etc/cron.d/ directory. Each user may have their own crontab entry and files related to each users are available in /var/spool/.

A crontab entry looks as follows:

Crontab entries

We can see from the preceding screenshot that a crontab entry has five asterisks. Each asterisk defines a specific duration. We can replace * with a value suggested against each of them or leave it as it is. If * is mentioned in a field, then it means consider all the instances of that field.

The timing syntax can also be described as follows:

  • Specify the minutes value between 0 to 59
  • Specify hours that can range from 0 to 23
  • Specify days that can range from 1 to 31
  • Specify months that can range from 1 to 12 or we can write Jan, Feb, … Dec
  • Specify the day of a week that can range from 0 to 6 or we can write sun (0), mon (1), …, sat (6)

All five fields are separated by blank spaces. It is followed by a username that specifies by which user the command will be executed. Specifying the username is optional and by default it is run as a root. The last field is command that is scheduled for execution.

An example demonstrating how to write the crontab entry is as follows:

20 7 * * 0 foo command

Each field can be explained as follows:

  • 20: 20th minute
  • 7: 7AM
  • *: Each day
  • *: Each month
  • 0: On Sunday
  • foo: This command will run as the foo user
  • command: This is the specified command to be executed

So, the command will run as root at 7:20 AM every Sunday.

We can specify multiple instances of a filed using a comma (,):

30 20,22 * * * command

Here, command will run at 8:30 PM and 10:30 PM every day.

We can also specify a range of time in a field using a hyphen (-) as follows:

35 7-11 * * 0-3 command

This means, the run command is at 7:35, 8:35, 9:35, 10:35, and 11:35 on Sunday, Monday, Tuesday, and Wednesday.

To run a script at a specific interval, we can specify the forward slash (/) as follows:

20-45/4 8 9 4 * command

The command will run on 9th April between 8:20 AM to 8:45 AM at an interval of 4 minutes.

Special strings in Crontab

Crontab may have the following strings specified as well:

String

Description

@hourly

Run once in an hour, equivalent to 0 * * * *

@daily or @midnight

Run once in a day, equivalent to 0 0 * * *

@weekly

Run once in a week, equivalent to 0 0 * * 0

@monthly

Run once in a month, equivalent to 0 0 1 * *

@yearly or @annually

Run once in a year, equivalent to 0 0 1 1 *

@reboot

Run at system start-up

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.239.100