The reasons why a cron job may fail is really very large. Most common reasons may be,
- Syntax error
- Using executable without proper path
- Using a shell variable which is not defined in cron environment
- Attempt to open a GUI without specifying
DISPLAY
And so on...
Sometimes it is really difficult to understand which error is preventing the job to be executed. Is there a way to detect error(s) in a cron jobs?
Setup Local Mail and get cron job output mails
When a cron job starts successfully or fails to execute for some reason cron daemon automatically sends an email to the user who set the cron job. That mail contains the description of a successful job or the detailed report why the particular cron job failed.
If your local mail system is not set you will not receive those mails. The cron job output emails will just die in a local mail spool folder. So you need to set up your mailer daemon to read those mails. See this answer on How read local email in thunderbird to get all your local mails.
If your cronjob fails you will receive a mail stating the reason of failure. This is one of the best way to detect the error in setting a cron job.
Alternate ways
According to this answer one can get errors of a cronjob in a log file using redirection. But you need to set the redirection with your cron job and specify the log file by yourself.
And the
/var/log/syslog
file is always there to check if your cron job is running as you expected or not.There is an extensive answer here that explains how to make the 16.04 LTS system create a separate cron.log file, and how to make a command
wcron
that watches it in real-time.16.04: How do I make cron create cron.log and monitor it in real time?
It also includes information for changing the log levels so you can see not only the script launches (default), but also the end times and errors.
It clearly shows any errors they generate.