86

I have added a crontab entry on a Linux server that will run a Java executable. The Java code uses its own class for logging errors and messages into a log file.

But when I checked the log file after the scheduled time, no messages were logged. There should have been at least one log message saying the execution had started.

So there are two possible causes:

  1. The code executed but didn't log;
  2. Or, the code didn't execute at all.

The log file specified has chmod 777 permissions so I'm guessing it's the second cause here.

Why wouldn't a crontab job execute at its scheduled time? And how do I debug this without any kind of logging happening?

I have read that if there is an error cron sends an email to the user. How do I find out which email address is associated with the user?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
naiquevin
  • 7,588
  • 12
  • 53
  • 62

6 Answers6

146

You can enable logging for cron jobs in order to track problems. You need to edit the /etc/rsyslog.conf or /etc/rsyslog.d/50-default.conf (on Ubuntu) file and make sure you have the following line uncommented or add it if it is missing:

cron.*                         /var/log/cron.log

Then restart rsyslog and cron:

sudo service rsyslog restart
sudo service cron restart

Cron jobs will log to /var/log/cron.log.

jpihl
  • 7,941
  • 3
  • 37
  • 50
Mehdi Yedes
  • 2,267
  • 2
  • 14
  • 14
  • 3
    After I did this, I could find this log. `(CRON) info (No MTA installed, discarding output)`. so I installed postfix then I could see some logs by `less /var/mail/` – Kennyhyun Sep 11 '21 at 13:16
50

Append 2>&1 to the end of your Crontab command. This will redirect the stderr output to the stdout. Then ensure you're logging the crontab's Unix command.

0 0,12 1 */2 * ( /sbin/ping -c 1 192.168.0.1; ls -la ) >>/var/log/cronrun 2>&1

This will capture anything from the Unix command.

A couple of additional hints (after helping a colleague the other day ...). Write out the environment variables by issuing the command set with no parameters. And get the shell to echo each command with the set -x command. At the top of your script issue;

set
set -x
slm
  • 15,396
  • 12
  • 109
  • 124
Karl
  • 3,312
  • 21
  • 27
  • it still doesnt log anything. The crontab command is `25 08 * * * /root/java/MyProject/sync.sh >> /root/java/Migration/sync.log 2>&1`. If I execute the shell script however, then the code runs and the java logger class writes to the log file – naiquevin Feb 03 '11 at 08:24
  • Are you setting environment variables in the script? Often if a script runs from the command line but not from cron it can be environment. have synch.sh write its env output to the log. – Karl Feb 03 '11 at 09:00
  • which variables will I need to set ? I tried `env` command from the terminal and it doesnt show either JAVA_HOME or CLASSPATH. This is the live server where I have just installed JRE and not JDK. Thanks – naiquevin Feb 03 '11 at 10:37
  • ok.. it was a very silly mistake from my side. completely missed that vi editor had commented out lines in crontab ! .. sorry for bothering. – naiquevin Feb 03 '11 at 12:03
14

For anyone else struggling with this. Cronjobs log themselves in /var/mail/{username} even if they don't do stdoutput

Michael Eliot
  • 831
  • 8
  • 18
7

Assuming that running the command manually works, but not in Cron, it may be that the correct path is not exposed to the cron command. You can fix this by running crontab -e and then entering the path directly into the cron tab:

# Export the path so that the scripts run correctly PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin:/root/bin

user189271
  • 178
  • 1
  • 7
3

One thing that may be causing your problem is that cron (at least in the distribution I am using, Amazon Linux OS) consider times to be in UTC, therefore if you are in a different timezone (e.g. -03:00) you may be expecting it to run 3 hours in advance that it actually will and in fact you don't have any problem.

Renato Gama
  • 16,431
  • 12
  • 58
  • 92
2

Check if you really formatted the time when it has to run well.

For example instead of

*/1 * * * * echo 'debug' > /home/glab/change_branch.log

might be this:

1 * * * * echo 'debug' > /home/glab/change_branch.log

and you might be expecting it to run every minute. And also no logs are generated.

Dariux
  • 3,953
  • 9
  • 43
  • 69