Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I ran my crontab job 0 2 */1 * * /aScript >aLog.log 2>&1 as a 'root' user, and however I found the env is different from env of the 'root' user, and therefore experiencing a different runtime behavior of my scripts.

An attempt fix was placing export commands in rc.d files, but it still didn't show up! I end up placing export commands in the aScript itself.

My question is that is there a better way to approach this problem? and why env is missing even though it is from the same user 'root' ? (I modifies crontab by running 'crontab -e' from the root)

share|improve this question
Cron always runs with a mostly empty environment. HOME, LOGNAME, and SHELL are set; and a very limited PATH. If you do not want to set all the variables yourself, you may be able to source your (bash) profile. –  cyberx86 Dec 5 '11 at 6:18
@cyberx86: Why not write that up as an answer and get rep ? –  Iain Dec 5 '11 at 8:16
@Iain: rep is always welcome - but sometimes it feels like a one line answer doesn't really earn the rep. I fully accept that a succinct answer has its place, but I have been (perhaps incorrectly) using comments as an 'easy way out' when I wanted to provide some assistance, but not write up a full, detailed explanation (it was 1am...). I will however take your advice and expand on this one a bit and add it as an answer. –  cyberx86 Dec 5 '11 at 21:17
@cyberx86: Better to have a correct one liner than incorrect or nothing. –  Iain Dec 5 '11 at 21:20

3 Answers 3

up vote 8 down vote accepted

Cron always runs with a mostly empty environment. HOME, LOGNAME, and SHELL are set; and a very limited PATH. It is therefore advisable to use complete paths to executables, and export any variables you need in your script when using cron.

There are several approaches you can use to set your environment variables in cron, but they all amount to setting it in your script.

Approach 1: Set each variable you need manually in your script.

Approach 2: Source your profile:

. $HOME/.bash_profile (or . $HOME/.profile)

(You will ususally find that the above file will source other files (e.g. ~/.bashrc --> /etc/bashrc --> /etc/profile.d/*) - if not, you can source those as well.)

Approach 3: Save your environment variables to a file (run as the desired user):

env > /path/to/

Then import via your cron script:

env - `cat /path/to/` /bin/sh

Approach 4: In some cases, you can set global cron variables in /etc/default/cron. There is an element of risk to this however, as these will be set for all cron jobs.

share|improve this answer

Cron creates its OWN shell with the use specified through which it will run.

So, if you want to keep the same variable of your user, then try to run it with your own user, instead of root or any other user.


The best way is that export those variables in your own script.

share|improve this answer

In RedHat CentOS, you could set /etc/rc.d/init.d/functions default PATH to permanently set. /etc/rc.d/crond calls functions when starts.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.