Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
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
add comment

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

share|improve this answer
    
+1 for good intentions (seriously, though, for the absolute paths) –  Dennis Williamson Jul 24 '09 at 13:29
add comment

In the crontab file, you can specify environment variables:

FOO=bar

0 * * * * echo $FOO

The above should echo "bar" every hour.

share|improve this answer
add comment

Your Answer

 
discard

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.