VOOZH about

URL: https://www.geeksforgeeks.org/linux-unix/how-to-setup-cron-jobs-in-ubuntu/

⇱ Setting Up Cron Jobs in Ubuntu - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Setting Up Cron Jobs in Ubuntu

Last Updated : 13 Feb, 2026

A cron job is a scheduled task that executes commands or scripts at defined times. It is managed by the cron service on Linux systems. Cron jobs are essential for automating routine tasks, improving system reliability, and reducing manual intervention.

  • Runs commands automatically at scheduled times
  • Managed by the cron daemon/service
  • Can execute scripts, system commands, or programs
  • Useful for routine tasks like backups, updates, and log rotation
  • Requires correct permissions to run scripts or commands

Benefits of Using Cron Jobs in Ubuntu

Cron jobs provide a reliable way to automate repetitive tasks, ensuring efficiency and consistency. They are widely used by system administrators and developers for routine system management.

Setting Up Cron Jobs on Ubuntu

Cron jobs are configured using the cron service and the crontab file. The setup involves checking system updates, ensuring cron is installed and running, and then defining scheduled tasks.

Step 1: Connect to the server and update the system

Before configuring cron jobs, make sure your system is up to date.

apt-get update && apt-get upgrade
  • apt-get update: Updates the package list
  • apt-get upgrade: Upgrades installed packages to the latest versions

Step 2: Verify if cron is installed

To check if cron is installed, run the following command

dpkg -l cron

If cron is not installed:

sudo apt-get install cron

Step 3: Check if the cron service is running

To check whether the cron service is running on the system, we can use the following command

systemctl status cron

If it is inactive, start it with:

sudo systemctl start cron

Step 4: Configure cron jobs

Cron jobs are added either through user-specific crontabs or the system crontab (/etc/crontab). Only root can modify the system crontab.

Editing the system crontab (root only):

sudo nano /etc/crontab

Editing a user crontab (recommended for non-root tasks):

crontab -e

Cron Syntax Overview:

Each cron job follows this pattern:

* * * * * /path/to/command arg1 arg2
  • * (Minute) : Minute of execution [0–59]
  • * (Hour) : Hour of execution [0–23]
  • * (Day of month) : Day of the month [1–31]
  • * (Month) : Month of execution [1–12]
  • * (Day of week) : Day of the week [0–7] (0 or 7 = Sunday)
  • /path/to/command arg1 arg2 : Command or script to execute

Practical Examples of Cron Jobs

Example 1: Monthly Backup

Schedule a backup script to run at 9:00 AM on the first day of every month.

crontab -e

Append the following entry:

0 9 1 * * /path/to/backup-script.sh
  • 0: Minute (0th minute)
  • 9: Hour (9 AM)
  • 1: Day of month (1st)
  • *: Month (every month)
  • *: Day of week (any day)
  • /path/to/backup-script.sh: Script to execute

The script runs automatically at 9:00 AM on the first day of every month. No terminal output unless the script generates logs.

Example 2: Daily PHP Script

Set up and run PHP script as a cron job to run the script every day at 10 AM.

crontab -e

Append the following entry:

0 10 * * * /usr/bin/php /path/to/myphpscript.php
  • 0: Minute (0th minute)
  • 10: Hour (10 AM)
  • *: Day of month (every day)
  • *: Month (every month)
  • *: Day of week (every day)
  • /usr/bin/php /path/to/myphpscript.php: Execute PHP script

PHP script executes daily at 10:00 AM. Logs or script output depend on the script itself.

Cron Command Options

  • crontab -l: List all current user cron jobs
  • crontab -r: Remove all current user cron jobs
  • man cron: View cron service manual
  • man crontab: View crontab manual.

Troubleshooting and Best Practices for Cron Jobs

When running cron jobs, issues can occur due to permissions, paths, or environment variables. Following best practices helps avoid failures.

Key Troubleshooting Tips

  • Use absolute paths: Always specify the full path to scripts and commands (e.g., /usr/bin/php). Relative paths may fail because cron runs with a minimal environment.
  • Check file permissions: Ensure scripts are executable: chmod +x /path/to/script.sh
  • Define environment variables: Cron jobs run in a limited environment; variables like PATH may not be inherited. Set variables at the top of your crontab if needed.
  • Test scripts manually first: Run scripts from the terminal to verify they work before scheduling in cron.
  • Check cron logs for errors: Cron writes logs to /var/log/syslog (Ubuntu/Debian). Example: grep CRON /var/log/syslog
  • Combine with log rotation for logs: Use logrotate to prevent log files from growing too large.

Best Practices

  • Use user-specific crontabs whenever possible; avoid running everything as root.
  • Keep cron commands short and simple; call scripts for complex operations.
  • Avoid running multiple heavy tasks at the same time to reduce system load.
  • Comment cron entries for clarity:
# Backup script runs on the 1st of every month at 9 PM
0 21 1 * * /path/to/backup-script.sh
Comment
Article Tags:

Explore