How to Fix Linux Cron Job Not Running Error
In this tutorial, you'll learn about How to Fix Linux Cron Job Not Running Error. We cover key concepts, practical examples, and best practices.
You set up a cron job but it never runs — the cron service may not be running, the crontab syntax is wrong, or the script fails because of missing environment variables.
The Problem
crontab -l
Shows:
* * * * * /home/user/backup.sh
But backup.sh never executes.
Step-by-Step Fix
Step 1: Check if cron service is running
sudo systemctl status cron
If inactive:
sudo systemctl start cron
sudo systemctl enable cron
Step 2: Check cron logs
sudo grep CRON /var/log/syslog | tail -20
Or:
sudo journalctl -u cron -n 20
Step 3: Verify crontab syntax
# WRONG — no minute/hour specified
* * * * * command
# RIGHT — every day at 2:30 AM
30 2 * * * /home/user/backup.sh
Validate with:
crontab -l | grep -v '^#' | while read line; do echo "$line" | crontab -; done
Step 4: Use absolute paths in scripts
# WRONG — relative path, no PATH
* * * * * backup.sh
# RIGHT — absolute path with PATH
PATH=/usr/local/bin:/usr/bin:/bin
* * * * * /home/user/backup.sh >> /home/user/backup.log 2>&1
Step 5: Make script executable
chmod +x /home/user/backup.sh
Step 6: Add logging to debug
* * * * * /home/user/backup.sh >> /tmp/cron-debug.log 2>&1
Check the log after the job should have run.
Step 7: Check user permissions
# Check if the script is readable by the cron user
ls -la /home/user/backup.sh
Step 8: Use full interpreter path
#!/bin/bash
Not:
#!/usr/bin/env bash # may not resolve in cron's minimal PATH
Prevention Tips
- Always use absolute paths in cron jobs
- Redirect output to a log file for debugging
- Set PATH explicitly at the top of crontab
- Test scripts manually before adding to cron
- Use
MAILTO=user@example.comto get error emails
Common Mistakes with cron not running
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
These mistakes appear frequently in real-world LINUX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro