Cron Job Management: Creating Scheduled Tasks
Create and manage scheduled tasks on Linux with crontab: cron expression syntax, practical examples, logging output, and avoiding common pitfalls.
Table of Contents
Cron Job Management: Creating Scheduled Tasks
Cron is a time-based job scheduler in Linux that automatically runs commands and scripts at specified times or intervals. It is essential for automating repetitive tasks like backups, log cleanup, and update checks.
The cron service is installed and running by default on most Linux distributions. Check its status with systemctl status cron or systemctl status crond.
Crontab Syntax
Each cron job consists of five time fields and the command to run:
* * * * * /path/to/command
│ │ │ │ │
│ │ │ │ └── Day of week (0-7, 0 and 7 = Sunday)
│ │ │ └──── Month (1-12)
│ │ └────── Day of month (1-31)
│ └──────── Hour (0-23)
└────────── Minute (0-59)
Special Characters
| Character | Meaning | Example |
|---|---|---|
* | Any value | * * * * * = every minute |
, | Multiple values | 1,15 * * * * = at minute 1 and 15 |
- | Range | 1-5 * * * * = minutes 1 through 5 |
/ | Step | */5 * * * * = every 5 minutes |
Practical Cron Expression Examples
# Run every minute
* * * * * /usr/bin/script.sh
# Run at the top of every hour
0 * * * * /usr/bin/script.sh
# Run every day at 02:30
30 2 * * * /usr/bin/backup.sh
# Run every Monday at 08:00
0 8 * * 1 /usr/bin/weekly-report.sh
# Run on the 1st of every month at midnight
0 0 1 * * /usr/bin/monthly-cleanup.sh
# Run every 5 minutes
*/5 * * * * /usr/bin/check-service.sh
# Run on weekdays (Mon-Fri) at 09:00
0 9 * * 1-5 /usr/bin/workday-task.sh
# Run every 6 hours
0 */6 * * * /usr/bin/sync.sh
Special Shortcuts
@reboot # Once at system startup
@yearly # Once a year (0 0 1 1 *)
@monthly # Once a month (0 0 1 * *)
@weekly # Once a week (0 0 * * 0)
@daily # Once a day (0 0 * * *)
@hourly # Once an hour (0 * * * *)
Using crontab
Editing Crontab
# Edit the current user's crontab
crontab -e
# Edit a specific user's crontab (requires root)
crontab -u username -e
# List current crontab
crontab -l
# Remove crontab (use with caution!)
crontab -r
The crontab -r command deletes all your cron jobs and cannot be undone. Back up first with crontab -l > crontab_backup.txt.
Editing Crontab for the First Time
# Run crontab -e
crontab -e
# If prompted to choose an editor, select nano (option 1)
# Add your job at the end of the file:
30 2 * * * /home/user/backup.sh
# Save and exit: Ctrl+X, Y, Enter (for nano)
System-Wide Cron Files
System-wide cron jobs use the /etc/cron* directories:
# Directory structure
/etc/crontab # Main system crontab file
/etc/cron.d/ # Application cron files
/etc/cron.hourly/ # Scripts run hourly
/etc/cron.daily/ # Scripts run daily
/etc/cron.weekly/ # Scripts run weekly
/etc/cron.monthly/ # Scripts run monthly
# /etc/crontab format (includes user field)
30 2 * * * root /usr/bin/backup.sh
Cron Logging
Monitoring Log Files
# Cron logs on Debian/Ubuntu
sudo tail -f /var/log/syslog | grep CRON
# On RHEL/CentOS
sudo tail -f /var/log/cron
# With journald
sudo journalctl -u cron -f
sudo journalctl -u crond -f
Redirecting Output to a File
# Redirect both stdout and stderr to a file
30 2 * * * /usr/bin/backup.sh >> /var/log/backup.log 2>&1
# Log only errors
30 2 * * * /usr/bin/backup.sh 2>> /var/log/backup-errors.log
# Suppress all output
30 2 * * * /usr/bin/backup.sh > /dev/null 2>&1
Configure logrotate to rotate log files so they don't grow indefinitely.
Common Pitfalls and Solutions
1. Environment Variable Issues
Cron does not inherit the user shell's environment variables. Define PATH and other variables explicitly:
# Add to the top of your crontab
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
SHELL=/bin/bash
# Or use full paths in commands
30 2 * * * /usr/bin/python3 /home/user/script.py
2. Script Execute Permission
# Grant execute permission to the script
chmod +x /home/user/backup.sh
# Verify permissions
ls -la /home/user/backup.sh
3. Working Directory Issues
# Explicitly set the working directory in the cron job
30 2 * * * cd /var/www/html && php artisan schedule:run
# Or use bash -c
30 2 * * * /bin/bash -c 'cd /var/www/html && php artisan schedule:run'
4. Checking the Cron Service
# Check if cron is running
sudo systemctl status cron # Debian/Ubuntu
sudo systemctl status crond # RHEL/CentOS
# Restart the service
sudo systemctl restart cron
Testing a Cron Job
# Test the script manually first
bash /home/user/backup.sh
# Simulate the cron environment
env -i HOME=/root SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin /home/user/backup.sh
# Redirect cron output to a file instead of email
MAILTO=""
30 2 * * * /home/user/backup.sh >> /tmp/cron-test.log 2>&1
Conclusion
Cron is one of the fundamental tools in Linux system administration. With correct syntax, environment variable management, and logging, you can build reliable automation. Regularly review your jobs with crontab -l and monitor log files to catch issues early.
Related Articles
How to Connect to Your VDS Server via SSH (Complete Guide)
Step-by-step SSH connection guide for Linux VDS: PuTTY, Terminal, key-based authentication, Path Panel firewall rules, and security best practices.
How to Connect to Windows VDS via RDP: Complete Guide
Complete guide to connecting to Windows Server VDS via Remote Desktop (RDP): setup steps, NLA security, Path Panel firewall rule, and troubleshooting tips.
How to Change VDS Server OS: Step-by-Step Reinstall Guide
Step-by-step guide to changing your VDS server OS via REXE panel: backup tips, supported Linux distributions and Windows Server versions explained.