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
The
cron
utility runs under a very limited environment. This means that you have to spell things out in a way you would not if you were working interactively at the command line (e.g.,/usr/bin/perl
rather than justperl
).It's also worth saying that Apple recommends
launchd
overcron
. (I actually usecron
myself on Macs, but only because I'm used to it from Linux machines. I keep meaning to switch.)In the
crontab
file, you can specify environment variables:The above should echo "bar" every hour.
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.