Sometimes its useful to know what environment variables cron has to debug/troubleshoot something.
There used to be a page that had some nifty info on how to do this but it has gone offline. There’s a Wayback Machine link here buts its slow so I’m going to reproduce it here for convenience.
This is the scenario: you want to set a cron job and the script you want to use runs correctly when called from the command line, but when you run the same script with crontab, it does not work.
More than likely, this problem is caused by the default shell environment variables setup by crontab. Cron supplies a default (bare) environment for every shell:
HOME=user's-home-directory LOGNAME=user's-login-id PATH=/usr/bin:/usr/sbin:. SHELL=/usr/bin/sh
Here are a few solutions:
- Explicitly enter the needed environment variables.
- Load your .profile in the crontab or in the script called by crontab.
- Use the absolute path for every command run in the script.
Here is a simple test to know exactly what variables are different, or even missing, in the crontab environment:
From the shell prompt, enter:
$ env > /tmp/myenv.log
Then set a cron job to do the same from the crontab.
$crontab -e * * * * * env > /tmp/crontabenv.log :wq
This line in the crontab will execute every minute, and store the environment variables in /tmp/crontabenv.log . diff both files to find the differences.
For more info about how to edit and use the crontab do: $man crontab