Home > Package & System Management > cron

cron: Daemon for Scheduled Task Execution

cron is a daemon (background service) in Linux/Unix systems that automatically executes commands or scripts at specified times. Users or system administrators typically register and manage tasks (cron jobs) through the crontab command. This daemon starts automatically at system boot, runs in the background, and periodically checks for and executes registered tasks.

Overview

The cron daemon is a core system service used for automating repetitive tasks. Users can configure commands to be executed, the execution time, and the frequency through crontab files. There are system-wide crontab files (/etc/crontab) and individual user crontab files.

Key Features

  • Time-based task scheduling
  • User-specific and system-wide task management
  • Automatic execution in the background
  • Used for regular system maintenance and automation

Key Options

The cron daemon typically starts automatically at system boot, and it's rare to execute it directly from the command line. The options below are mainly used for manually starting or debugging the cron daemon. Users primarily manage cron jobs using the crontab command.

Daemon Control and Debugging

Generated command:

Try combining the commands.

Description:

`cron` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

It is uncommon to directly execute the cron daemon itself. Typically, its status is checked via systemctl, and jobs are managed through crontab.

Check cron service status

systemctl status cron

Verifies if the cron daemon is currently running.

Edit current user's crontab file

crontab -e

Adds, modifies, or deletes cron jobs for the current user.

List current user's crontab jobs

crontab -l

Displays all cron jobs registered for the current user.

View system crontab file content

cat /etc/crontab

Inspects system-wide cron jobs. (Use caution when modifying)

Restart cron service

sudo systemctl restart cron

Restarts the cron service to apply configuration changes.

Tips & Precautions

Tips and precautions for efficiently and safely managing cron jobs.

Key Tips

  • **Environment Variables**: The shell environment variables in a crontab environment can be limited. It's recommended to explicitly set necessary environment variables within scripts or use full paths.
  • **Log Checking**: If cron jobs do not execute as expected, check logs using syslog or `journalctl -u cron` to diagnose the issue.
  • **Output Redirection**: By default, the standard output (stdout) and standard error (stderr) of cron jobs are mailed to the user running the job. To prevent unnecessary emails, redirect output to `/dev/null` or save it to a log file (e.g., `command > /path/to/log.log 2>&1`).
  • **Using @reboot**: The `@reboot` keyword can be used for tasks that should run only once after system reboot.
  • **Crontab Syntax**: Accurately understand and use the schedule syntax in the format `minute hour day month day_of_week`.

Same category commands