I'm working on a project where I need to execute about a dozen different cron jobs that trigger commands in a PHP project. The PHP source depends on certain environment variables to distinguish dev/test/production environments and behave accordingly.
I just figured out that scripts executed from cron don't benefit from the export commands I've put into the .bashrc file. Apparently cron jobs don't source the .bashrc file.
I could add a another command to each line in my crontab, but that seems like a LOT of redundant code -- and also vulnerable to errors:
Code:
* * * * * source ~/.bashrc;/usr/bin/php /path/to/foo.php &> /path/to/output/foo-output.txt
* * * * * source ~/.bashrc;/usr/bin/php /path/to/bar.php &> /path/to/output/bar-output.txt
* * * * * source ~/.bashrc;/usr/bin/php /path/to/answer42.php &> /path/to/output/answer42-output.txt
Is there some better way to inject environment variables into my php script? Either
1) export my environment variables in some other script (.bash_profile? .environment? other?) so that these vars are available to all crontab commands
executed by one particular user
2) add the command
source ~/.bashrc somewhere so that these environment variables will be in effect for all of my cron jobs
3) Inject this environment variable into PHP somehow before my php files execute?
Any advice would be much appreciated.