What the 'Cron Job Failed' Error Means
The "Cron Job Failed" error is a general message from the cron daemon indicating that a scheduled task did not execute successfully. It is usually accompanied by a more detailed message in the logs or an email notification (if configured). The error occurs when the command or script specified in the crontab exits with a non-zero return code or cannot be launched due to access, path, or environment issues. Typical symptoms: the task does not run on schedule, logs contain entries with "failed" or "error", and the user receives an email with the error text.
Common Causes
Specific causes of the "Cron Job Failed" error include:
- Incorrect path to the script or command: The crontab uses a relative path (e.g.,
./script.sh) or a path to a non-existent file. Cron does not interpret relative paths from the current directory. - Missing execute permissions: The script lacks execute permission (x) for the user under which cron runs. This is a common issue after copying files or changing ownership.
- Errors within the script itself: Syntax errors (e.g., in a bash script), missing dependencies (installed packages or modules), runtime errors (division by zero, missing files), or an incorrect return code.
- Cron's limited environment: Cron runs with a minimal set of environment variables (e.g., PATH is often limited to
/usr/bin:/bin). If the script uses commands located in/usr/local/binor other directories, they won't be found, causing a "command not found" error. - Insufficient resources: The system may run out of memory, disk space, or hit process limits (ulimit), causing the task to be terminated.
- Incorrect crontab format: The schedule syntax (five time fields) is invalid, leading to the line being ignored or executed unexpectedly.
- Conflicts with other processes: If the task is long-running and cron is set to frequent launches, conflicts can arise from simultaneous access to resources (files, sockets).
- Directory access permission issues: The cron user must have execute permission on all parent directories in the script's path; otherwise, access will be denied.
- Shebang line error in the script: If the script's first line (e.g.,
#!/bin/bash) points to a non-existent interpreter, cron cannot launch it. - System restrictions: On some distributions, cron may be disabled for users (settings in
/etc/cron.allowor/etc/cron.deny), or the task may exceed limits defined in/etc/security/limits.conf.
Solutions
Method 1: Analyze Cron Logs
The first step is always to check the cron logs for the exact error message. The logs contain details that point to the specific cause.
For systemd systems (Ubuntu 16.04+, CentOS 7+, Fedora):
journalctl -u cron -b --no-pager | tail -50
Or for syslog (traditional Debian/Ubuntu):
grep CRON /var/log/syslog | tail -50
In the output, look for lines related to your task (by time or script name) and the error code, e.g., "No such file or directory" or "Permission denied". This will immediately narrow down the problem.
Method 2: Correct Paths and Permissions
Ensure the crontab specifies an absolute path to the script or command. Relative paths do not work in cron.
Example of a correct entry:
* * * * * /home/user/scripts/backup.sh
Verify the file exists and check its permissions:
ls -l /home/user/scripts/backup.sh
The output should include x for owner/group/others (e.g., -rwxr--r--). If permissions are missing, add them:
chmod +x /home/user/scripts/backup.sh
Also check permissions on parent directories:
namei -l /home/user/scripts/backup.sh
All path components must have execute permission (x) for the cron user.
Method 3: Manual Script Testing
Run the script from the command line as the same user that cron uses. If cron is configured for user user, execute:
sudo -u user /home/user/scripts/backup.sh
Or, if you are already logged in as that user:
/home/user/scripts/backup.sh
If the script requires environment variables (e.g., PATH, HOME, or custom variables), export them before running or configure them in the crontab (see Method 5). Manual execution will reveal syntax errors or missing dependencies.
Method 4: Validate Crontab Syntax
Open the crontab for editing:
crontab -e
Ensure each line follows the format:
minute hour day_of_month month day_of_week command
For example, * * * * * means "every minute". Common errors: using invalid values (e.g., 70 for minutes), missing fields, extra spaces. To validate, you can use online validators or run:
crontab -l | crontab -
If the syntax is incorrect, crontab will reject the changes.
Method 5: Configure Environment Variables
Cron launches tasks with a minimal environment (usually only HOME, LOGNAME, PATH=/usr/bin:/bin). If the script uses commands from /usr/local/bin or other variables (e.g., JAVA_HOME), set them in the crontab.
Add at the top of the crontab:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOME=/home/user
Or for a specific script:
* * * * * . $HOME/.profile; /home/user/scripts/backup.sh
Variables can also be set explicitly within the script itself.
Method 6: Check Resources and Limits
Ensure the user has sufficient resources. Check disk quotas and memory:
df -h # free disk space
free -h # available memory
Also check process limits for the user in /etc/security/limits.conf or via ulimit -a (when running as that user). If the task requires significant memory or processes, increase the limits.
Method 7: Prevent Concurrent Runs
If the task takes longer than the cron interval, conflicts can arise from parallel instances. Use the flock utility to create a lock:
* * * * * flock -n /tmp/backup.lock /home/user/scripts/backup.sh
The -n option prevents waiting if the lock is already held, causing the task to be skipped. This ensures only one script instance runs at a time.
Method 8: Enable Error Notifications
Configure email alerts on failures to be promptly notified of issues. In the crontab, set the MAILTO variable:
MAILTO=user@example.com
* * * * * /home/user/scripts/backup.sh
Ensure an MTA (e.g., Postfix or Sendmail) is configured on the system. If email is not needed but logging is, redirect output to a file:
* * * * * /home/user/scripts/backup.sh > /var/log/backup.log 2>&1
Avoid redirecting to /dev/null in production, as this will hide errors.
Prevention
To avoid recurring "Cron Job Failed" errors:
- Always use absolute paths in crontab for scripts and commands.
- Set execute permissions on scripts (
chmod +x) and verify permissions on parent directories. - Test scripts manually as the cron user before adding them to crontab, including checking dependencies.
- Explicitly define environment variables in crontab (PATH, HOME) or within scripts, especially when using non-standard commands.
- Regularly review cron logs (
/var/log/syslogorjournalctl) for errors, even if tasks appear to be working. - Configure email notifications for critical tasks to respond quickly to failures.
- Use locking mechanisms (e.g.,
flock) for long-running tasks to prevent concurrent executions. - Document crontab with comments explaining each task's purpose and expected outcomes.
- After system updates, verify that interpreter paths (e.g.,
/bin/bashvs/usr/bin/bash) or dependencies have not changed.