Using Cron in Linux: Scheduling Tasks (deprecated)

This article is deprecated

Most modern systems have deprecated cron. Please see Systemd timers.

Introduction to Cron

Cron is a time-based job scheduler in Linux that allows users to schedule tasks to run periodically at fixed times, dates, or intervals. It's an essential tool for automating repetitive tasks such as system maintenance, backups, and script execution.

Basic Cron Syntax

Cron jobs are defined using these fields:

1* * * * * command_to_execute
2- - - - -
3| | | | |
4| | | | +----- Day of the week (0 - 7) (Sunday is 0 or 7)
5| | | +------- Month (1 - 12)
6| | +--------- Day of the month (1 - 31)
7| +----------- Hour (0 - 23)
8+------------- Minute (0 - 59)

Scheduling Tasks with Cron

1. Editing the Crontab

To edit the current user's crontab, use the crontab -e command:

1user@machine:~$ crontab -e

This opens the crontab file in the default text editor specified by the EDITOR environment variable. You can dynamically set this so it opens in nano with the following command:

1user@machine:~$ EDITOR=nano crontab -e

2. Adding a Cron Job

To add a new cron job, append a line to the crontab file in the following format:

1* * * * * command_to_execute

Replace * * * * * with the desired schedule and command_to_execute with the command or script to be executed.

3. Examples of Cron Entries

  • Run a script every hour:
10 * * * * /path/to/script.sh
  • Run a script every day at midnight:
10 0 * * * /path/to/script.sh
  • Run a script every Monday at 3:30 AM:
130 3 * * 1 /path/to/script.sh

Managing Cron Jobs

1. Listing Cron Jobs

To view the list of cron jobs for the current user, use the crontab -l command:

1user@machine:~$  crontab -l

2. Removing Cron Jobs

To remove a cron job, use the crontab -e command and remove the entry.

To remove all cron jobs, use the crontab -r command:

1user@machine:~$  crontab -r

Troubleshooting Cron

1. Checking Cron Logs

Cron logs are typically stored in the Systemd Journal, /var/log/syslog, or /var/log/cron. Check these logs for any errors or issues with cron jobs.

2. Verifying Environment Variables

Ensure that cron jobs specify full paths to commands and scripts, as cron jobs run with a limited environment compared to interactive shells. If a script is being execute, ensure that it has the execute permission set.

Conclusion

Cron is a powerful tool for automating tasks in Linux. By understanding cron syntax and how to schedule, manage, and troubleshoot cron jobs, you can streamline system administration and improve productivity. Experiment with cron to automate routine tasks and simplify maintenance activities on your Linux system.