Debugging Cron

September 06, 2017

Context
Cron job wasn’t running. Couldn’t tell why.
I had done the following in the crontab:

Still did not work.

Logging

I then found out that I could enable logging for cron by:

[whatever cmd] >> /home/dhruv/projects/prod/cron.log 2>&1

File Descriptor 1 = stdout
File Descriptor 2 = stderr

Basically, stdout is being now being redirected to the log file. And the `2>&1’ is redirecting stderr to stdout (which is the log file)

Using this, I found out that my chromedriver wasn’t on the PATH!

Environment variables

cron has a limited set of environmental variables. You can check by adding a cronjob like so:

* * * * * env > /tmp/env.output

Now you can check which environment variables are set. In my case, only /usr/bin and /bin were set.
My chromedriver binary was in /usr/local/bin.

Solution

Two ways I could solve this - 1) add chromedriver to cron’s PATH or 2) change cron’s path to include /usr/local/bin.

I chose to go with 2). This can be done by setting the PATH variable at the top of the crontab:

PATH=/usr/local/bin:/usr/sbin:/usr/bin:/bin

I just set it to whatever my bash PATH was.