Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging crontab jobs

Tags:

linux

cron

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?

like image 644
naiquevin Avatar asked Feb 03 '11 06:02

naiquevin


People also ask

How do I check cron jobs?

How to test a Cron Job? Open the Corntab – Its an online tool that will help you to Check the Cron time. You can enter the cron time and it will tell you when this cron will trigger. Note down the time and verify if its correct one.

How do I know if a cron job has failed?

Check that your cron job is running by finding the attempted execution in syslog. When cron attempts to run a command, it logs it in syslog. By grepping syslog for the name of the command you found in a crontab file you can validate that your job is scheduled correctly and cron is running.

How do I see crontab scripts?

View Current Logged-In User's Crontab entries : To view your crontab entries type crontab -l from your unix account. View Root Crontab entries : Login as root user (su – root) and do crontab -l. To view crontab entries of other Linux users : Login to root and use -u {username} -l.

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .


2 Answers

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.

like image 158
Mehdi Yedes Avatar answered Sep 28 '22 07:09

Mehdi Yedes


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 
like image 30
Karl Avatar answered Sep 28 '22 06:09

Karl