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


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.

  • What you describe as problematic is the defined behavior for cron jobs. – Telemachus Jul 24 '09 at 11:38
  • 1
    +1 for putting in your script. I've had mixed results with environment variables, so I like to make sure that everything I need is already defined. Also a good practice if you have jobs that should eventually run on startup. Your account may have the vars, but at startup they may not exist (or root may not have them). So I'm always in the habit of defining everything I need. It also helps to avoid any binary collisions (if you have multiple Perl packages installed, etc.). – Milner Jul 24 '09 at 13:04
  • I've decided to do it this way. I did try it the way Greg Hewgill suggested, but for some reason the variables still wern't showing up in the (php) script that was being executed. I've now created a simple php script that uses putenv() to set up the variables, and then includes the actual script I want to execute, so my cron task is something like: php setup.php script_to_execute.php. Probably not the perfect solution, but it works. – Jack Sleight Jul 24 '09 at 13:58

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 just perl).

It's also worth saying that Apple recommends launchd over cron. (I actually use cron myself on Macs, but only because I'm used to it from Linux machines. I keep meaning to switch.)

  • +1 for good intentions (seriously, though, for the absolute paths) – Dennis Williamson Jul 24 '09 at 13:29

In the crontab file, you can specify environment variables:


0 * * * * echo $FOO

The above should echo "bar" every hour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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