Skip to content

Cron Jobs and Task Scheduling

Category: Intermediate Linux Commands
Type: Linux Commands
Generated on: 2025-07-10 03:10:37
For: System Administration, Development & Technical Interviews


This cheatsheet provides a comprehensive guide to using cron jobs and task scheduling in Linux, focusing on practical usage for system administrators and developers.

  • cron: The primary daemon responsible for running scheduled tasks. It reads configuration files (crontabs) to determine which commands to execute and when.
  • crontab: A command-line utility to manage user-specific and system-wide cron tables (crontabs). Crontabs define the schedule and commands to be executed.
  • at: Schedules a command to be executed once at a specific time. Useful for one-off tasks.
  • batch: Similar to at, but executes the command when system load permits (generally, when the load average is below 1.5).

When to Use:

  • cron: Recurring tasks (e.g., daily backups, log rotation, system updates).
  • at: One-time tasks scheduled for a specific time in the future (e.g., running a long-running script overnight).
  • batch: Non-urgent tasks that can be executed opportunistically when system resources are available.
Terminal window
crontab [options] [file]
Terminal window
at time [date] [options]
Terminal window
batch [options]
  • Edit the current user’s crontab:

    Terminal window
    crontab -e

    This opens the crontab file in your default editor (usually vi or nano). The crontab format is:

    minute hour day_of_month month day_of_week command

    Example crontab entry:

    0 3 * * * /path/to/my/script.sh # Runs script at 3:00 AM every day
  • List the current user’s crontab:

    Terminal window
    crontab -l

    Example output:

    0 3 * * * /path/to/my/script.sh
  • Remove the current user’s crontab:

    Terminal window
    crontab -r

    WARNING: This will delete all scheduled tasks. Use with caution.

  • Edit a specific user’s crontab (requires root privileges):

    Terminal window
    sudo crontab -u username -e
  • Add a cron job from a file:

    Terminal window
    crontab my_cron_file
  • Schedule a command to run at 5:00 PM today:

    Terminal window
    at 17:00

    The shell will prompt you for the command:

    at> /path/to/my/script.sh
    at> <EOT> # Press Ctrl+D to finish
    job 1 will be executed at 2024-01-05 17:00
  • Schedule a command to run at 8:00 AM tomorrow:

    Terminal window
    at 08:00 tomorrow
  • Schedule a command to run on January 10th at 9:00 AM:

    Terminal window
    at 09:00 2024-01-10
  • Schedule a command to run in 2 hours:

    Terminal window
    at now + 2 hours
  • List pending at jobs:

    Terminal window
    atq

    Example output:

    1 2024-01-05 17:00 a user
  • Remove a pending at job:

    Terminal window
    atrm 1 # Removes job with ID 1
  • Run a command in batch mode:

    Terminal window
    batch

    The shell will prompt you for the command:

    batch> /path/to/my/resource_intensive_script.sh
    batch> <EOT> # Press Ctrl+D to finish
    job 2 will be run when system load is low.
  • -e: Edit the crontab.
  • -l: List the crontab.
  • -r: Remove the crontab.
  • -u username: Specify the user for whom to edit/list/remove the crontab (requires root privileges).
  • -i: Prompt before removing the crontab.
  • -f file: Read the command from a file instead of standard input.
  • -m: Send mail to the user when the job completes (even if there’s no output).
  • -v: Show the time the job will be executed.
  • -f file: Read the command from a file instead of standard input.
  • -v: Show the time the job will be executed.
  • Using special strings:

    • @reboot: Run once after every boot.
    • @yearly or @annually: Run once a year (0 0 1 1 *).
    • @monthly: Run once a month (0 0 1 * *).
    • @weekly: Run once a week (0 0 * * 0).
    • @daily or @midnight: Run once a day (0 0 * * *).
    • @hourly: Run once an hour (0 * * * *).

    Example:

    @reboot /path/to/startup_script.sh
  • Using ranges and steps:

    • */5 * * * *: Run every 5 minutes.
    • 0 9-17 * * 1-5: Run at minute 0 of hours 9 through 17 on weekdays (Monday-Friday).
    • 0 0 1,15 * *: Run at midnight on the 1st and 15th of every month.
  • Redirecting output:

    0 3 * * * /path/to/my/script.sh > /path/to/my/script.log 2>&1 # Redirect output and errors to a log file
    0 3 * * * /path/to/my/script.sh > /dev/null 2>&1 # Discard output and errors
  • Setting environment variables:

    SHELL=/bin/bash
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    MAILTO=admin@example.com # Send cron output to this email address (optional)
    0 3 * * * /path/to/my/script.sh
  • Running commands as a different user within a cron job:

    Terminal window
    # Must be in root's crontab (sudo crontab -u root -e)
    0 3 * * * su - otheruser -c "/path/to/my/script.sh"
  • Chaining commands with at:

    Terminal window
    at 10:00 <<EOF
    command1
    command2
    EOF
  • batch can read commands from a file, useful for larger, more complex tasks.
  • Use full paths: Always use absolute paths to commands and scripts in your crontab. This avoids issues with environment variables and current working directory.

  • Test your scripts: Before scheduling a script, test it thoroughly to ensure it runs as expected.

  • Log everything: Implement logging in your scripts to help with debugging. Redirect cron output to log files.

  • Monitor cron: Check the system logs (usually /var/log/syslog or /var/log/cron) for cron-related errors.

  • Stagger jobs: Avoid scheduling many jobs to run at the same time, especially at the top of the hour, to prevent performance bottlenecks.

  • Use descriptive comments: Add comments to your crontab to explain what each job does.

  • Disable email notifications: If you don’t need email notifications, set MAILTO="" in your crontab. This prevents unnecessary emails.

  • Use flock to prevent overlapping jobs: If a script might take longer to run than the scheduled interval, use flock to prevent multiple instances from running concurrently.

    Terminal window
    0 * * * * flock -n /tmp/my_script.lock /path/to/my/script.sh

    This creates a lock file /tmp/my_script.lock. If the script is already running, flock will prevent a new instance from starting until the lock is released. The -n option makes it non-blocking.

  • Cron job not running:
    • Check the system logs (/var/log/syslog or /var/log/cron) for errors.
    • Verify that the cron daemon is running (systemctl status cron).
    • Ensure the script has execute permissions (chmod +x /path/to/my/script.sh).
    • Double-check the crontab syntax for errors.
    • Make sure the script is not exiting prematurely due to an error. Add error handling to your script.
    • Verify that the user account used for the cron job exists and is not locked.
    • Check if SELinux or AppArmor is preventing the script from running.
  • Cron job runs but doesn’t produce the expected results:
    • Use absolute paths in the script.
    • Set the necessary environment variables in the crontab.
    • Check the script’s output and error logs.
    • Ensure the script has the necessary permissions to access files and resources.
  • Email not being sent:
    • Verify that a mail transfer agent (MTA) like Postfix or Sendmail is installed and configured correctly.
    • Check the MAILTO variable in the crontab.
    • Examine the mail server logs for errors.
  • at job not running:
    • Check the atq command to see if the job is still in the queue.
    • Verify that the atd daemon is running (systemctl status atd).
    • Check the system logs for errors.
    • Ensure the time is correct on the system.
  • Permissions issues:
    • Ensure the user running the cron job has the necessary permissions to execute the script and access any required files or resources.
    • Use sudo sparingly and only when absolutely necessary. If sudo is required, ensure the user running the cron job is authorized to use sudo without a password.
  • systemctl: Used to manage system services, including cron and atd.
  • journalctl: Used to view system logs, including cron and atd logs.
  • logrotate: Used to manage log file rotation. Often used in conjunction with cron.
  • anacron: Another cron-like utility designed for systems that are not always running (e.g., laptops). Anacron ensures that scheduled tasks are executed even if the system was offline when they were supposed to run. Anacron is often preferred over cron for systems that are not continuously running.
  • systemd timers: A modern alternative to cron jobs, integrated with systemd. Timers offer more flexibility and control over scheduling. See man systemd.timer and man systemd.time for more information.

This cheatsheet provides a solid foundation for working with cron jobs and task scheduling in Linux. Remember to consult the man pages for each command for more detailed information and options. Always test your scheduled tasks thoroughly before deploying them to a production environment.