How to automate tasks in Linux with cron

As a sysadmin, you may often need to perform tasks on your Linux system at regular intervals, such as backing up data, checking for updates, or cleaning up log files. One way to automate these tasks is to use the cron daemon, which is a built-in Linux utility that runs processes on a schedule.


As a sysadmin, you may often need to perform tasks on your Linux system at regular intervals, such as backing up data, checking for updates, or cleaning up log files. One way to automate these tasks is to use the cron daemon, which is a built-in Linux utility that runs processes on a schedule.

In this article, we’ll cover the basics of using cron to schedule tasks in Linux, including how to set up cron jobs and troubleshoot common problems.

We’ll also provide some example scripts to illustrate how cron can be used in practice.

What is cron and how does it work?

Cron is a daemon (a background process) that runs on a Linux system and executes commands at specified intervals. It does this by reading a configuration file called the crontab, which contains a list of commands and the times at which they should be run.

The crontab file is divided into lines, with each line representing a separate job. Each line consists of six fields:

  1. Minute (0-59)
  2. Hour (0-23)
  3. Day of the month (1-31)
  4. Month (1-12)
  5. Day of the week (0-6, with 0 representing Sunday)
  6. Command to be executed

These fields can contain either a single value (e.g. “30” for the minute field) or a range of values (e.g. “0-59” for the minute field). You can also use special characters such as “” (to specify all values) or “/2″ (to specify every other value) in the fields.

For example, here is a crontab line that runs a script called “backup.sh” every day at midnight:

0 0 * * * /path/to/backup.sh

And here is a crontab line that runs a script called “cleanup.sh” every other hour:

0 */2 * * * /path/to/cleanup.sh

Important notes

In Linux, there are three levels of users that can run cron jobs: root, system users, and normal users.

  1. Root user: The root user is the superuser in Linux, with full privileges to access and modify all system resources. Cron jobs that are run as the root user have full system privileges, and can therefore perform any task on the system. However, it is generally a good practice to avoid running cron jobs as the root user unless it is absolutely necessary, as it can be a security risk to grant such broad privileges to a script.
  2. System users: System users are users that are created by the system for specific purposes, such as running services or performing system tasks. System users typically have more restricted privileges than the root user, and are often used to run cron jobs that need to perform specific tasks but do not require full system privileges.
  3. Normal users: Normal users are regular users that are created by the root user or another system user. Normal users have limited privileges, and can only perform tasks that they have been granted permission to do. Cron jobs that are run as normal users are restricted to the privileges of the user they are running under.

In general, it is best to run cron jobs as system users or normal users, rather than as the root user, to minimize security risks and ensure that the jobs have only the necessary privileges to perform their tasks.

How to set up a cron job

There are several ways to set up a cron job, depending on the scope and duration of the task. Here are three common methods:

  1. Editing the system-wide crontab file

To set up a cron job that runs as the root user (i.e. with full system privileges), you can edit the system-wide crontab file. This file is typically located at /etc/crontab and can only be edited by the root user.

To edit the system-wide crontab file, you can use the crontab command with the -e option:

sudo crontab -e

 

 

This will open the crontab file in a text editor, where you can add your cron job. For example:

0 0 * * * /path/to/backup.sh

Save the file and exit the editor to apply your changes.

  1. Editing a user’s crontab file

To set up a cron job that runs as a specific user, you can edit that user’s crontab file. Each user on a Linux system has their own crontab file, which can be edited using the crontab command without any options:

crontab -e

 

This will open the user’s crontab file in a text editor, where you can add your cron job. For example:

0 0 * * * /path/to/backup.sh

Save the file and exit the editor to apply your changes.

  1. Using the at command

If you only need to run a cron job once, you can use the at command to schedule it for a specific time in the future. The at command allows you to specify a date and time, and it will run the specified command at that time.

To use the at command, enter the command at followed by the time you want the job to run. You will be prompted to enter the command you want to run, followed by an EOF marker (Ctrl+D). For example:

at 12:00 AM tomorrow 
/path/to/backup.sh
EOF

 

This will run the backup.sh script at midnight tomorrow.

You can also specify a date and time in the future using the at command’s -f option, followed by the path to a file containing the command you want to run:

at -f /path/to/backup.sh 12:00 AM tomorrow

 

 

Note that the at command is not installed by default on all Linux systems, so you may need to install it first.

Example scripts

Now that we’ve covered the basics of setting up cron jobs, let’s look at some example scripts that demonstrate how cron can be used in practice.

  • Backing up a MySQL database

Here is a simple script that uses the mysqldump utility to create a backup of a MySQL database:

#!/bin/bash

# Set the current date
date=$(date +"%Y-%m-%d")

# Set the backup directory
backup_dir="/path/to/backup/directory"

# Set the MySQL credentials
mysql_user="mysql_user"
mysql_password="mysql_password"

# Set the database name
database_name="database_name"

# Create the backup file name
backup_file="$backup_dir/$database_name-$date.sql"

# Use mysqldump to create the backup
mysqldump -u $mysql_user -p$mysql_password $database_name > $backup_file

# Compress the backup file
gzip $backup_file

To set up a cron job to run this script daily, you could add the following line to your crontab file:

0 0 * * * /path/to/backup_script.sh

This will run the script every day at midnight, creating a daily backup of the MySQL database.

  • Cleaning up log files

Here is a script that searches for log files in a specified directory and deletes any that are older than a certain number of days:


#!/bin/bash

# Set the directory containing the log files
log_dir="/path/to/log/directory"

# Set the age (in days) of the log files to delete
age=7

# Find and delete the log files
find $log_dir -type f -mtime +$age -exec rm {} \;

 

To set up a cron job to run this script daily, you could add the following line to your crontab file:

0 0 * * * /path/to/cleanup_script.sh

 

This will run the script every day at midnight, deleting any log files that are more than seven days old.

  • Sending a notification email

Here is a script that uses the mail command to send an email notification:


#!/bin/bash

# Set the email address to send the notification to
email_address="user@example.com"

# Set the subject of the email
subject="Cron job notification"

# Set the message of the email
message="This is a notification that the cron job has completed successfully."

# Send the email
echo $message | mail -s "$subject" $email_address

 

To set up a cron job to run this script after another script has completed, you could add a line to your crontab file that runs the notification script with the && operator:

0 0 * * * /path/to/main_script.sh && /path/to/notification_script.sh

 

This will run the main script first, and if it completes successfully, it will run the notification script.

Troubleshooting cron problems

If you encounter problems with your cron jobs, there are a few things you can check to troubleshoot the issue:

  1. Check the system logs

If a cron job is not running as expected, you can check the system logs to see if there are any error messages related to the job. The system logs are typically located in the /var/log directory, and you can use the grep command to search for specific messages.

For example, to search for errors related to the cron daemon, you can use the following command:

grep CRON /var/log/syslog
  1. Check the crontab file for syntax errors

Cron jobs will not run if there are syntax errors in the crontab file. To check for syntax errors, you can use the crontab -l command to list the contents of the crontab file, and look for any mistakes.

For example, if you forget to include the command to be executed in a crontab line, you will see an error message like this:

crontab: line X: no command
  1. Check the permissions of the script being run

Cron jobs will not run if the script being executed does not have the proper permissions. Make sure the script has execute permissions set for the user that is running the cron job.

To set execute permissions for a script, you can use the chmod command:

 chmod +x /path/to/script.sh 

Conclusion

In this article, we covered how to use the cron daemon to automate tasks on a Linux system. We discussed the basic syntax of crontab files and how to set up cron jobs using the crontab command and the at command. We also provided several example scripts that demonstrate how cron can be used in practice, and discussed some common issues that may arise when using cron and how to troubleshoot them.

By using cron to automate tasks, sysadmins can save time and effort, and ensure that important tasks are performed consistently and reliably.

Resources

  • The crontab and at  man page – the official documentation for the crontab command, and provides detailed information on the syntax and usage of crontab files. You can access the man page by typing man crontab at the command prompt.
  • The Linux Documentation Project – a comprehensive resource for Linux documentation, including guides, tutorials, and HOWTOs on a wide range of topics, including system administration and shell scripting.
  • The Bash Hackers Wiki – a community-driven resource for Bash scripting, with articles and tutorials on a variety of topics, including shell scripting, cron, and system administration.
  • The Linux Academy – a paid resource that provides online training and certification courses on Linux and related technologies, including system administration, shell scripting, and cron.