What the message looks like
CRON[2418]: (root) CMD (/usr/local/bin/job.sh)
/usr/local/bin/job.sh: 3: mysqldump: not foundCommon causes
- Cron has a different
PATHthan your shell and does not read.bashrc. - The script uses relative paths, so it runs in the wrong directory.
- The execute bit is missing or the shebang points to a missing interpreter.
- The entry is in the wrong user's crontab, or in
/etc/cron.dwithout the user column. - Cron itself is not running, or a syntax error prevents the file from loading.
Check
- Cron service and recent runs
systemctl status cron --no-pager | head -n 5; journalctl -u cron --since '1 hour ago' --no-pager | tail -n 20 - Entries of the current user
crontab -l - Permissions and shebang
ls -l /usr/local/bin/job.sh; head -n 1 /usr/local/bin/job.sh - Test with an empty environment like cron
env -i /bin/sh -c '/usr/local/bin/job.sh'
Fix
- Use absolute paths in the script and set
PATHat the top:PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin. - Redirect output to a log file:
>> /var/log/job.log 2>&1. Otherwise you never see the errors. - Check the shebang (
#!/bin/bash) and set the execute bit withchmod +x. - In
/etc/cron.ddo not forget the user column, otherwise the line is ignored. - Test with
env -iso you get the same environment as cron.
Typical scenario
A script backup.sh sits in /etc/cron.daily/ and runs perfectly by hand, but is never executed. On Debian and Ubuntu, run-parts processes this directory and ignores files with a dot in the name, backup.sh included. The file must also be executable. Check with run-parts --test /etc/cron.daily which files would actually be called. Files in /etc/cron.d, in contrast, have an extra field for the user, and their names must not contain a dot either.
FAQ
Why does it work manually but not from cron?
Because cron starts without your shell environment: different PATH, no home directory, no aliases.
Where does the output go?
Cron mails it to the local user. Without a mail server it disappears, so always log to a file.
How do I tell whether cron started the job at all?
Look in the system log, for example with journalctl -u cron or grep CRON /var/log/syslog. There is one line per start with user and command. If it is missing, the problem lies in the crontab; if it is there, it is the script or its environment.
Why does my command break with a percent sign?
In a crontab % is a newline and must be protected with a backslash. date +%F therefore becomes date +\%F. Without protection the command receives only the part before the percent sign, and the rest is passed as input.
Why does the job run at an unexpected time?
Cron uses the system's time zone. If the server runs in UTC, the time differs from your local time. At the switch to or from daylight saving time, a job between 2 and 3 a.m. can be skipped or run twice, so schedule critical jobs outside that window.