Skip to content

cron

Learn how to schedule and automate tasks in Linux using cron jobs.

Commands

Common commands for checking, editing and managing cron jobs.

Check cron service

Ensure that the cron service is installed and running.

Terminal window
systemctl status cron

Edit user crontab

Open the crontab editor for the current user.

Terminal window
crontab -e

List scheduled jobs

View existing cron jobs for the current user.

Terminal window
crontab -l

Remove all jobs

Clear the user’s crontab.

Terminal window
crontab -r

Automate with system-wide crontab

System jobs can be placed in /etc/crontab

Terminal window
sudo nano /etc/crontab

Syntax

Cron jobs use a five-field time format followed by the command to execute.

Terminal window
# ┌───────────── minute (0–59)
# │ ┌───────────── hour (0–23)
# │ │ ┌───────────── day of month (1–31)
# │ │ │ ┌───────────── month (1–12)
# │ │ │ │ ┌───────────── day of week (0–6, Sun=0)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command_to_execute

Examples

Common examples of scheduling tasks with cron.

Run script every day at midnight

Terminal window
0 0 * * * /home/username/backup.sh

Run a task every 15 minutes

Terminal window
*/15 * * * * /usr/bin/python3 /home/username/task.py

Weekly cleanup task

Terminal window
0 3 * * 0 root /usr/bin/apt autoremove -y