The Ultimate Guide to Using cron for Task Scheduling in Unix-based Systems

Introduction to cron

cron is a time-based job scheduling utility in Unix-based operating systems. It allows users to run scripts or commands at specified times and intervals. The scheduled tasks are called “cron jobs.” This article provides an in-depth guide to cron with various API examples to help you get the most out of task scheduling.

What is cron?

cron is a built-in UNIX utility that runs processes on your system at predefined times or intervals. This tool is often used for system maintenance or administration processes, such as running backups, clearing out logs, or sending emails.

Basic Usage

To start using cron, you need to edit the cron table or “crontab.” Each line in the crontab file represents a cron job, and each job is defined by six fields:

    * * * * * command_to_run
    - - - - -
    | | | | |
    | | | | ----- Day of week (0 - 7) (Sunday = 0 or 7)
    | | | ------- Month (1 - 12)
    | | --------- Day of month (1 - 31)
    | ----------- Hour (0 - 23)
    ------------- Minute (0 - 59)

Common cron Syntax Examples

    # Run a job every minute
    * * * * * /path/to/script.sh
    
    # Run a job every hour
    0 * * * * /path/to/script.sh
    
    # Run a job every day at midnight
    0 0 * * * /path/to/script.sh
    
    # Run a job at 2:30 PM on the first day of every month
    30 14 1 * * /path/to/script.sh

    # Run a job at 2:00 AM every Saturday
    0 2 * * 6 /path/to/script.sh

    # Run multiple scripts with one cron
    0 5 * * * /path/to/first_script.sh && /path/to/second_script.sh
    
    # Log output from cron jobs
    0 5 * * * /path/to/script.sh > /path/to/logfile.log 2>&1

Special Cron Keywords

There are also several special cron keywords that can be used instead of the five fields:

    @reboot    : Run once after reboot
    @yearly    : Run once a year (0 0 1 1 *)
    @annually  : Same as @yearly
    @monthly   : Run once a month (0 0 1 * *)
    @weekly    : Run once a week (0 0 * * 0)
    @daily     : Run once a day (0 0 * * *)
    @midnight  : Same as @daily
    @hourly    : Run once an hour (0 * * * *)

Advanced Scheduling with cron

cron supports advanced time intervals and more complex scheduling scenarios.

    # Run a job every 15 minutes
    */15 * * * * /path/to/script.sh
    
    # Run a job every 5th day of the month at 3:00 AM
    0 3 5 */1 * /path/to/script.sh
    
    # Run a job every weekday (Mon-Fri) at 2:00 PM
    0 14 * * 1-5 /path/to/script.sh
    
    # Run a job every two hours
    0 */2 * * * /path/to/script.sh

Application Example: Backup Script

Let’s create a backup script and schedule it to run daily using cron.

Step 1: Create the Backup Script

    #!/bin/bash
    # backup.sh - A simple backup script

    # Define backup directory and source directory
    BACKUP_DIR="/backups"
    SOURCE_DIR="/var/www"

    # Create a dated backup tarball
    DATE=$(date +\%Y-\%m-\%d)
    tar -czvf $BACKUP_DIR/backup-\$DATE.tar.gz $SOURCE_DIR

Step 2: Make the Script Executable

    chmod +x /path/to/backup.sh

Step 3: Schedule the Script in crontab

    crontab -e
    # Add the following line to run the script every day at 2 AM
    0 2 * * * /path/to/backup.sh

This setup ensures that your website files are backed up every day at 2 AM.

Keep experimenting with different cron schedules to automate tasks and improve your workflow.

Hash: c374eea599bb3c50ed4432ef57aee1263cdc3c83d1c1a3c4d7e19eef1af8439b

Leave a Reply

Your email address will not be published. Required fields are marked *