Crontab
Crontab is the program to manage cron jobs for individual users. Edit, list, and remove scheduled tasks.
Basic Crontab Commands
| Command | Description |
|---|---|
crontab -e | Edit current user’s crontab |
crontab -l | List current user’s cron jobs |
crontab -r | Remove current user’s crontab (deletes all jobs) |
crontab -i | Interactive remove (asks for confirmation) |
crontab -v | Show last edit date and time |
crontab -u [user] | Edit/list crontab for specific user (requires sudo) |
crontab [file] | Install crontab from file |
Editing Crontab
# Edit crontab with default editor
crontab -e
# Use specific editor
EDITOR=nano crontab -e
EDITOR=vi crontab -e
# Change default editor
export EDITOR=nano
crontab -e
# View what editor is used
echo $EDITOR
Crontab File Format
# Format: minute hour day month weekday command
# Example:
0 9 * * 1-5 /home/user/backup.sh
# Comments start with #
# Blank lines are ignored
# One job per line
Crontab Examples
Time-Based Jobs
# Minute field (0-59)
0 9 * * * command # At 9:00
15 9 * * * command # At 9:15
*/5 9 * * * command # Every 5 minutes during 9 AM hour
# Hour field (0-23)
0 9 * * * command # At 9:00
0 21 * * * command # At 9:00 PM (21:00)
0 */4 * * * command # Every 4 hours
# Day field (1-31)
0 0 1 * * command # 1st of month
0 0 15 * * command # 15th of month
0 0 1,15 * * command # 1st and 15th
# Month field (1-12)
0 0 1 1 * command # January 1st
0 0 1 6 * command # June 1st
0 0 * 12 * command # Daily in December
# Weekday field (0-7, both 0 and 7 are Sunday)
0 0 * * 0 command # Sunday
0 0 * * 1 command # Monday
0 0 * * 1-5 command # Monday-Friday
0 0 * * 0,6 command # Saturday-Sunday (weekend)
Common Scheduling Patterns
# Every minute
* * * * * command
# Every 5 minutes
*/5 * * * * command
# Every hour at 15 minutes past
15 * * * * command
# Every day at 2:30 AM
30 2 * * * command
# Every Monday at 9 AM
0 9 * * 1 command
# Every 1st and 15th at midnight
0 0 1,15 * * command
# Every weekday (Monday-Friday) at 5 PM
0 17 * * 1-5 command
# Every 6 hours
0 */6 * * * command
# Every 2 hours during business hours (9 AM - 5 PM)
0 9-17/2 * * 1-5 command
# Run twice daily (9 AM and 5 PM)
0 9,17 * * * command
# First day of month at 1 AM
0 1 1 * * command
# Last day of month (approximation)
0 0 28-31 * * command
# Every Sunday and Friday at 10 PM
0 22 * * 0,5 command
Managing Multiple Users’ Crontabs
# View another user's crontab (requires sudo)
sudo crontab -u username -l
# Edit another user's crontab (requires sudo)
sudo crontab -u username -e
# Remove another user's crontab (requires sudo)
sudo crontab -u username -r
# Remove with confirmation (requires sudo)
sudo crontab -u username -i
# Install crontab for another user (requires sudo)
sudo crontab -u username -i crontab_file
Working with Crontab Files
Export Crontab
# Save current crontab to file
crontab -l > my_crontab.txt
# Save another user's crontab
sudo crontab -u username -l > username_crontab.txt
Import Crontab
# Install crontab from file (replaces existing)
crontab my_crontab.txt
# Install for another user
sudo crontab -u username username_crontab.txt
# Append to existing crontab
crontab -l > temp.txt
cat additional_jobs.txt >> temp.txt
crontab temp.txt
rm temp.txt
Add Single Job to Crontab
# Method 1: Using echo
(crontab -l 2>/dev/null; echo "0 9 * * * /backup.sh") | crontab -
# Method 2: Edit file then import
crontab -l > cron_backup.txt
echo "0 9 * * * /backup.sh" >> cron_backup.txt
crontab cron_backup.txt
rm cron_backup.txt
# Method 3: Using heredoc
(crontab -l 2>/dev/null || true; cat <<EOF
0 9 * * * /home/user/backup.sh
0 0 * * 0 /home/user/weekly-task.sh
EOF
) | crontab -
Remove Specific Job from Crontab
# Remove jobs matching pattern
crontab -l | grep -v "backup.sh" | crontab -
# Remove and keep a backup
crontab -l > cron_backup.txt
crontab -l | grep -v "backup.sh" | crontab -
# List jobs to remove first
crontab -l | grep "backup"
Crontab Environment Variables
# Set environment variables at top of crontab
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
MAILTO=admin@example.com
HOME=/home/username
# Then define jobs
0 9 * * * /backup.sh
0 0 * * 0 /maintenance.sh
# To view existing environment
crontab -l | head -10
Output Redirection
# Send output to file
0 9 * * * /backup.sh > /var/log/backup.log 2>&1
# Append to log file
0 9 * * * /backup.sh >> /var/log/backup.log 2>&1
# Discard output
0 9 * * * /backup.sh > /dev/null 2>&1
# Email output (requires MAILTO set)
0 9 * * * /backup.sh
# Mail output only if error
0 9 * * * /backup.sh || (echo "Backup failed on $(hostname)" | mail -s "Backup Alert" admin@example.com)
# Log with timestamp
0 9 * * * echo "$(date): Backup started" >> /var/log/backup.log && /backup.sh >> /var/log/backup.log 2>&1
Crontab Restrictions
# /etc/cron.deny - Users who cannot use crontab
# /etc/cron.allow - Only these users can use crontab
# /etc/crontab - System-wide cron jobs
# System crontab format (includes username):
0 9 * * * root /usr/local/bin/backup.sh
0 * * * * www-data /var/www/cron.php
Viewing Crontab
List Crontabs
# Current user
crontab -l
# Specific user (with sudo)
sudo crontab -u username -l
# All users' crontabs (from /var/spool/cron/crontabs)
sudo ls -la /var/spool/cron/crontabs/
# View specific user's crontab directly
sudo cat /var/spool/cron/crontabs/username
Find All Crontabs
# Search all user crontabs
for user in $(cat /etc/passwd | cut -f1 -d:); do
echo "=== $user ==="
sudo crontab -u $user -l 2>/dev/null
done
# List all active cron jobs
sudo cat /var/spool/cron/crontabs/*
Troubleshooting
Crontab Not Working
# Check if cron service is running
sudo systemctl status cron
sudo service cron status
# Start cron service
sudo systemctl start cron
sudo service cron start
# Enable on boot
sudo systemctl enable cron
# Check user is allowed to use crontab
sudo test -f /etc/cron.allow && cat /etc/cron.allow
sudo test -f /etc/cron.deny && cat /etc/cron.deny
View Cron Logs
# Ubuntu/Debian
sudo tail -f /var/log/syslog | grep CRON
# CentOS/RHEL/Fedora
sudo tail -f /var/log/cron
# macOS
log stream --level debug --predicate 'eventMessage contains[cd] "cron"'
# Using journalctl (newer systems)
journalctl -u cron -f
journalctl -u cron -n 50 # Last 50 entries
Debug Crontab Issues
# Check crontab syntax
crontab -l
# Test script manually
/home/user/script.sh
# Run with bash explicitly
/bin/bash -x /home/user/script.sh
# Add logging to script
#!/bin/bash
{
echo "Job started at $(date)"
# ... rest of script
echo "Job ended at $(date)"
} >> /var/log/myjob.log 2>&1
# Test environment
env > /tmp/cron_env.txt
Best Practices
- Always use absolute paths for commands and files
- Redirect output to log files
- Add comments explaining each job
- Test scripts manually before adding to crontab
- Use MAILTO for error notifications
- Backup crontab file:
crontab -l > crontab_backup.txt - Set proper permissions on scripts
- Use meaningful filenames in /tmp
- Log output for debugging
- Avoid using
crontab -rwithout backup - Include error handling in scripts
Common Issues & Solutions
# Issue: "No such file or directory"
# Solution: Use absolute path
0 9 * * * /home/user/backup.sh # Correct
0 9 * * * backup.sh # Wrong
# Issue: Script not executable
# Solution: Make executable
chmod +x /home/user/backup.sh
# Issue: Environment variables not found
# Solution: Set in crontab or script
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
0 9 * * * /backup.sh
# Issue: "Permission denied"
# Solution: Check file permissions
ls -la /home/user/backup.sh
chmod 755 /home/user/backup.sh
Resources
man crontab- Crontab manualman 5 crontab- Crontab file format- Crontab Generator
- Cron Syntax Checker
Last updated: 2026-03-30