Environment variables for a cronjob

It has happen to me too often that a script runs well at the command line when you test it but when you schedule it through cron it fails. After some troubleshooting I realize that it is caused by missing environment variable like PATH or JAVA_HOME.

After searching on the topic for a few minutes this morning I saw a recommendation that I adapted.

Before the script in crontab I just added:

source /home/user/.bashrc

This runs the .bashrc content and all my expected environment variables are there for the script to run the same way in cron as it does at the command line.

The entire cron line would look something like this:

50 3 * * *    source /home/user/.bashrc; /path/to/script 1>>/tmp/logfile.log 2>>/tmp/logfile.log

 

Advertisements

Published by

m5c

Java developper that loves photography and good coffee

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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