cron - Linux


Overview

The cron command is used in Linux and Unix-based systems to schedule tasks (cron jobs) to run at fixed times, dates, or intervals. It typically reads from the crontab (cron table) files and the /etc/crontab file, as well as the /etc/cron.*/* directories. cron is most commonly used for automating system maintenance or administration, though it can be used for other repetitive tasks like sending emails, backups, and file cleanups.

Syntax

The cron service does not have an interactive command syntax, since it runs as a daemon. However, users interact with it using the crontab files. Here is how you manage crontab entries:

crontab [-u user] [-l | -r | -e]
  • -u user specifies the crontab which belongs to the user; this option requires superuser privileges to modify another user’s crontab.
  • -l lists the crontab for the user.
  • -r removes the current crontab for the user.
  • -e edits the current crontab using the editor specified by the VISUAL or EDITOR environment variables.

Options/Flags

  • -u user: Edit or display another user’s crontab. This option requires administrative privileges.
  • -l: List the content of the crontab.
  • -r: Remove the current crontab.
  • -e: Edit the current crontab using the default system editor.

Examples

  1. Listing a Crontab

    crontab -l
    

    This command will display the current user’s crontab entries.

  2. Editing a Crontab

    crontab -e
    

    This will open the crontab in the default editor where you can add or modify scheduled tasks.

  3. Removing a Crontab

    crontab -r
    

    This command will delete the current user’s crontab.

  4. Scheduling a Job
    To run a script every day at 3 AM, you would add the following line to your crontab:

    0 3 * * * /usr/local/bin/daily-job.sh
    

Common Issues

  • Permissions: Make sure that the user has the necessary permissions to edit or schedule cron jobs.
  • Environment Variables: Cron jobs run in a minimal environment, so many environment variables that are available in an interactive shell might not be set.
  • Editor Issues: If your default editor is not set or is incorrectly configured, you might encounter issues editing the crontab. Ensure that the EDITOR or VISUAL environment variable is set to your preferred editor.

Integration

Cron can be combined effectively with shell scripts to automate complex tasks. Here’s a basic example where cron is used to perform a daily database backup:

  1. Create a backup script (/usr/local/bin/db_backup.sh):
    #!/bin/bash
    /usr/bin/pg_dump -U user dbname > /path/to/backup/db-$(date +%Y%m%d).sql
    
  2. Schedule the backup script using crontab:
    0 2 * * * /usr/local/bin/db_backup.sh
    

    This crontab entry schedules the script to run at 2 AM every day.

  • at: Executes commands at a specified time.
  • anacron: Used to run commands periodically with a frequency defined in days. Suited for systems that do not run 24 hours a day.
  • watch: Useful for running scripts or commands repeatedly at regular intervals within an active session.

For more information on cron, you can check the online manual using man 5 crontab or visit online resources like the official GNU documentation.