skip to content

Search

Syspirit
EN

Cron

Automated task scheduling on Linux with cron and crontab!

Linux
Published on

Cron is a program that automatically executes commands and scripts at regular intervals according to a defined schedule.

Crontab Management

ActionCommand
Edit crontabcrontab -e
List taskscrontab -l
Remove all taskscrontab -r
Edit for a usersudo crontab -e -u username
View user’s crontabsudo crontab -l -u username
Install from filecrontab file.txt
See who has crontabsls -la /var/spool/cron/crontabs/

Crontab Syntax

ElementPositionPossible Values
Minute10-59
Hour20-23
Day of month31-31
Month41-12 or JAN-DEC
Day of week50-7 or SUN-SAT

Format

 * * * * * command_to_execute
 | | | | |
 | | | | +-- Day of week (0-7, 0=Sunday)
 | | | +---- Month (1-12)
 | | +------ Day of month (1-31)
 | +-------- Hour (0-23)
 +---------- Minute (0-59)

Special Characters

CharacterMeaningExample
*All values* * * * *
,List of values0,15,30,45 * * * *
-Range of values1-5 * * * *
/Step/Interval*/15 * * * *
@rebootAt system startup@reboot script.sh

Common Schedules

ActionScheduleCron
Every minuteEach minute* * * * *
Every hourEach hour0 * * * *
Every day at 6:00 AMDaily0 6 * * *
Every Monday at 8:30 AMWeekly30 8 * * 1
First of each monthMonthly0 0 1 * *
Every 15 minutesQuarter hour*/15 * * * *
Every 5 minutesFive minutes*/5 * * * *
Every day at midnightDaily nocturnal0 0 * * *
Weekdays at 9:00 AMBusiness days0 9 * * 1-5

Practical Examples

ActionCron Command
Daily backup0 2 * * * /scripts/backup.sh
Weekly report0 8 * * 1 /scripts/weekly-report.sh
Log cleanup0 0 * * 0 /scripts/cleanup-logs.sh
File sync*/30 * * * * rsync -av /src/ /dest/
Daily email0 9 * * * /scripts/daily-email.sh
System health check*/10 * * * * /scripts/health-check.sh

Cron Locations: What’s the Difference?

User vs. System Crontab

MethodUsageAdvantagesDisadvantages
crontab -ePersonal user tasksIsolation, securityDeleted if user removed
/etc/crontabGlobal system tasksPersistent, rootSecurity risk, different format
/etc/cron.d/Applications/packagesModularityRequires system format
  • crontab -e -> Personal backups, user scripts, account maintenance
  • /etc/crontab -> Critical system tasks (cleanup, server maintenance)
  • /etc/cron.d/ -> Installed applications, shared admin scripts
  • /etc/cron.{daily,weekly}/ -> Simple scripts without precise timing

Different Format for System

# User crontab (crontab -e)
0 2 * * * /home/user/backup.sh
 
# System files (/etc/crontab, /etc/cron.d/)
0 2 * * * root /home/user/backup.sh
#         ^ user required

Files and Directories

File/DirectoryDescription
/etc/crontabMain system crontab
/etc/cron.d/Additional system crontabs
/etc/cron.hourly/Scripts run every hour
/etc/cron.daily/Scripts run every day
/etc/cron.weekly/Scripts run every week
/etc/cron.monthly/Scripts run every month
/var/spool/cron/crontabs/User crontabs
/var/log/cronCron execution logs

Monitoring and Logs

ActionCommand
View cron logssudo tail -f /var/log/cron
Search in logssudo grep "username" /var/log/cron
Cron service statussudo systemctl status cron
Start servicesudo systemctl start cron
Restart servicesudo systemctl restart cron
Reload configsudo systemctl reload cron
Stop servicesudo systemctl stop cron

Common Troubleshooting

ProblemSolution
Task not runningCheck logs and permissions
No email notificationConfigure system MTA
Permission issueUse absolute paths
Environment variablesDefine PATH in crontab
Wrong timezoneCheck timedatectl
Incorrect syntaxTest with crontab -l

Environment Variables

Useful variables in crontab

System Variables

# Set editor
EDITOR=nano
 
# Set paths
PATH=/usr/local/bin:/usr/bin:/bin
 
# Email for notifications
MAILTO=admin@example.com
 
# Shell to use
SHELL=/bin/bash
 
# Working directory
HOME=/home/user

Example with Variables

SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=admin@example.com
 
# Daily backup at 2:00 AM
0 2 * * * /scripts/backup.sh > /var/log/backup.log 2>&1
 
# Weekly cleanup
0 3 * * 0 /usr/bin/find /tmp -type f -mtime +7 -delete

Time Shortcuts

ShortcutEquivalentDescription
@yearly0 0 1 1 *Once a year
@annually0 0 1 1 *Once a year
@monthly0 0 1 * *Once a month
@weekly0 0 * * 0Once a week
@daily0 0 * * *Once a day
@midnight0 0 * * *Every day at midnight
@hourly0 * * * *Once an hour
@rebootAt startupAt system restart

Examples with Shortcuts

# Annual backup
@yearly /scripts/annual-backup.sh
 
# Weekly reboot
@weekly /sbin/reboot
 
# Daily cleanup
@daily /scripts/cleanup.sh