Cron Script Q

When logged in as root if I type "env" there are a bunch of environment settings including one for CLASSPATH. However, I ran a cron script that ran this command "env > cronEnv". I noticed that the environment variables were entirely different inside the script. There wasn't even a CLASSPATH defined. Can any explain to me why that is? It's weird that the user root vs a cron script ran under user root would have different paths. Can anyone explain how to rectify this situation? It's a bash shell in linux by the way.

cron makes an internal system call to create a process and start the script. The system call is execv - it "loses" environment variables. This is a very common problem for folks trying to use cron.

If you need environment variables, have cron call a script that sets up what it needs when it starts to run. When you login, if you don't run .profile, .bashrc, .cshrc, or /etc/profile you have no environment to speak of. cron jobs do not execute all of these login scripts by default, so they are like a user that doesn't 'login correctly' in that sense.

try starting your script with stuff like this, which is just an example, not a template:

#!/bin/ksh
. /etc/profile
. /path/to/.profile  
# do your stuff here

See this thread for an opposing view.