Day - 21 of DevOps

Day - 21 of DevOps

How to Automate Tasks with cron Jobs in Linux

What is a cron?

Cron is a job scheduling utility present in Unix-like systems, The cron reads the crontab (cron tables) for running predefined scripts.

Cron is named after the Greek word “Chronos” which is used for time. It is a system process that will automatically perform tasks as per the specific schedule.

By using a specific syntax, you can configure a cron job to schedule scripts or other commands to run automatically.

Any task that you schedule through crons is called a cron job. Cron jobs help us automate our routine tasks, whether they're hourly, daily, monthly, or yearly.

If you're working in IT, you might need to schedule various repetitive tasks as part of your automation processes

In Windows, its called "Task Scheduler "application its also similar to Cron jobs.

Why use Cron jobs?

Here are the reasons for using Cronjobs in Linux:

  • Helps OS to take a scheduled backup of log files or database.

  • Delete old log files

  • Archive and purge database tables

  • Send out any notification email such as Newsletters, Password expiration email

  • Regular clean-up of cached data

  • Crontab is an ideal option to automate Unix jobs.

  • It is used to automate system maintenance

Linux Crontab format

Crontab of Linux has six fields. The first five fields define the time and date of execution, and the 6’th field is used for command execution.

Crontab syntax:

The Ultimate Guide to Cron: Read and write cron expressions like a pro

How to add Crontab Jobs in Linux/Unix - TekkieHead

Writing your first Cron job

Cron jobs are managed through a special program called crontab, and every operating system user has their crontab file which is located in the /var/spool/cron/crontabs/ directory.

You can open up your crontab file using the following command:

Select /bin/nano and press Enter. Your default crontab editor will be set to Nano and a temporary crontab file will be created and opened in the editor. This file contains some text explaining a Cron expression's basic syntax. Note that any line beginning with the # character signifies a comment.

Create a script called date-time.sh which prints the system date and appends it to a dateout.txt file. The script is shown below:

Make the script executable by giving it execution rights.

Add the script in the crontab using crontab -e.

Here, we have scheduled it to run per minute.

Check the output of the file dateout.txt. According to the script, the system date should be printed to this file every minute.

Creating a Backup Script

  • Backups are essential for protecting your work. You can create a simple backup script that copies your important files to a safe location.

  • Example Backup Script: backup.sh

#!/bin/bash

# Define source and target directories
src=/home/gokul/DevOps
trg=/home/gokul/backupjob

# Ensure that the target directory exists
mkdir -p $trg

# Get the current timestamp
curr_timestamp=$(date "+%d-%m-%Y-%H-%M-%S")
# Create backup file name with timestamp
backup=$trg/$curr_timestamp.tgz

# Inform the user about the backup process
echo "Taking backup on $curr_timestamp"
#echo $backup

# Create the backup archive using tar
tar czf $backup --absolute-names $src
echo "backup complete"

  • This script compresses your files into a tarball with a timestamp and saves it to the backup destination.

  • Let's create a Cron job for backup files as mentioned in the script

  • This cron schedule will execute the backup.sh script every five minutes during the Sunday

    Happy Learning :)