Cron Not Working? Troubleshooting Guide

Your cron job isn't executing? Here are the most common reasons and solutions.

๐Ÿ” Common Issues

1. Missing Execute Permission

Your script needs executable permissions:

chmod +x /path/to/script.sh

2. Wrong PATH

Cron has a minimal PATH. Use absolute paths:

# Wrong
* * * * * script.sh

# Correct
* * * * * /usr/local/bin/script.sh

3. Missing Shebang

Your script needs a proper shebang:

#!/bin/bash
# Your code here

4. Environment Variables

Cron doesn't load your .bashrc or .profile. Set variables explicitly:

* * * * * PATH=/usr/local/bin:/usr/bin /path/to/script.sh

๐Ÿงช How to Debug

Check Cron Logs

# Ubuntu/Debian
tail -f /var/log/syslog | grep cron

# CentOS/RHEL
tail -f /var/log/cron

# macOS
log show --predicate 'process == "cron"' --last 1m

Test Your Command Manually

# Run exactly what cron would run
/bin/bash -c "your-command-here"

# Or run the script directly
/path/to/script.sh

Redirect Output to Log

* * * * * /path/to/script.sh >> /tmp/script.log 2>&1

๐Ÿ“‹ Cron Service Status

Check if Cron is Running

# Ubuntu/Debian
systemctl status cron

# CentOS/RHEL
systemctl status crond

# macOS
sudo launchctl list | grep cron

Restart Cron

sudo systemctl restart cron
sudo systemctl restart crond

โš ๏ธ Special Characters Gotchas

Percent Sign (%)

In cron, % has a special meaning (translates to newline). Escape it with backslash:

# Wrong
* * * * * echo "Date is %Y-%m-%d" >> log.txt

# Correct
* * * * * echo "Date is \%Y-\%m-\%d" >> log.txt

Question Mark (?)

Some systems don't support ?. Use * instead:

# Use * instead of ?
* * * * *

โœ… Quick Checklist