Inspect the environment vars in cron

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:


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

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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: