Cron – OS X cron and environment variables

cronenvironment-variablesmac-osx

I'm currently setting a number of environment variables in OS X in /etc/profile using the export command. These are all listed correctly when I check in terminal, however when a script is executed through cron none of these variables are set.

As I understand it cron runs with its own environment variables, but how can I get my custom ones into the cron environment as well?

Please explain in the simplest possible way, as you may have guessed, this isn't something I know a lot about.

Thanks,
Jack

Best Answer

I haven't checked in detail but the bash manpage explains (search for INVOCATION) that bash doesn't read /etc/profile when it's not invoked as a login or interactive shell.

The login program always launches the shell as a login shell so I guess cron does not and the shell doesn't read any profile files (according to the man page it would read the file given in the ENV variable when the shell was started but cron doesn't seem to have an option to set that).

I found it generally to be problematic to rely on any global settings (e.g. PATH) in cron job scripts so I define my own settings in every script.