LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-10-2017, 09:24 PM   #1
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Rep: Reputation: 78
cron doesn't source .bashrc -- how to do this for all commands in crontab?


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.
 
Old 04-11-2017, 01:50 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,235
Blog Entries: 10

Rep: Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160
all 3) points sound like viable solutions to me.

but the code you show looks ok to me; does it not work?
 
Old 04-11-2017, 03:08 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,373

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
The shell might be dash instead of bash, and .bashrc should normally only be used for interactive shells. But you can change it - something like this in the beginning of your crontab:

SHELL=/bin/bash
BASH_ENV=~/.bashrc

But I think it's probably better not to use .bashrc for this, and instead make some other script setting those variables.
 
1 members found this post helpful.
Old 04-11-2017, 09:29 AM   #4
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by ondoho View Post
all 3) points sound like viable solutions to me.

but the code you show looks ok to me; does it not work?
The code does seem to work, but is it really required to source the bashrc file on every single line? Is there no way to specify this value in a single spot?
 
Old 04-11-2017, 09:45 AM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,373

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Did you try to add SHELL and BASH_ENV in the beginning of your crontab?
 
Old 04-11-2017, 10:00 AM   #6
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by Guttorm View Post
Did you try to add SHELL and BASH_ENV in the beginning of your crontab?
Thanks for this suggestion. I've not yet tried this. I'm hoping to get more specifics about what those commands do. Is there any way to determine what shell is being used? Also, would assigning BASH_ENV displace any other configuration value or does it have an additive effect? This machine has a complex crontab and I want to avoid breaking other things.
 
Old 04-11-2017, 10:13 AM   #7
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,373

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
I think the default shell for cron is /bin/sh - which usually is a symlink to dash. Setting the SHELL variable forces all commands in the crontab to use bash instead, and setting BASH_ENV tells bash to source ~/.bashrc which it normally does not.

If all you need to do is to set some variable, you can also add variables with values in the beginning of your crontab, for example:
Code:
MY_CUSTOM_VARIABLE="Hello there"

* * * * * echo "$MY_CUSTOM_VARIABLE" >> /tmp/my_custom_log.txt
That's probably a safer way, since you don't change other things.
 
1 members found this post helpful.
Old 04-11-2017, 10:27 AM   #8
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Original Poster
Rep: Reputation: 78
OK finding some answers to my own questions here...
Quote:
Originally Posted by sneakyimp View Post
Is there any way to determine what shell is being used?
OK I added a quick cron line to try and figure out what shell is running. Is this correct?
Code:
*     *     *     *     *     echo $SHELL > /tmp/shell.txt
The result is:
Code:
/bin/sh
What sort of behavior changes might I expect when switching *all* of my cron jobs from /bin/sh to /bin/bash?

Quote:
Originally Posted by sneakyimp View Post
Also, would assigning BASH_ENV displace any other configuration value or does it have an additive effect? This machine has a complex crontab and I want to avoid breaking other things.
I'm guessing that defining BASH_ENV would not have especially far-reaching consequences, especially because my .bashrc file is so simple.

I found some interesting detail here. There's a lot of posts/pages that talk about this sort of thing, but it's hard to get a handle on exactly what gets changed.
 
Old 04-11-2017, 10:29 AM   #9
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by Guttorm View Post
If all you need to do is to set some variable, you can also add variables with values in the beginning of your crontab, for example:
Code:
MY_CUSTOM_VARIABLE="Hello there"

* * * * * echo "$MY_CUSTOM_VARIABLE" >> /tmp/my_custom_log.txt
That's probably a safer way, since you don't change other things.
Ooooooh! This looks super helpful. I am in fact really just trying to set one environment variable so that my PHP scripts know whether the system is dev/test/production. Thank you for this. I'm going to try it out right now.
 
Old 04-11-2017, 12:48 PM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,235
Blog Entries: 10

Rep: Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160
Quote:
Originally Posted by sneakyimp View Post
Is there any way to determine what shell is being used?
Code:
ls -la /bin/sh
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using source command in crontab doesn't work correctly massy Programming 9 07-01-2014 04:37 AM
/etc/crontab vs /etc/cron.d vs /var/spool/cron/crontabs/ drManhattan Linux - Newbie 9 01-04-2011 01:12 AM
root cron script using cp commands doesn't copy all files legacyprog Linux - General 6 04-27-2009 02:38 AM
bash's source command doesn't work with .bashrc crs_zxf Linux - Newbie 18 03-07-2009 02:25 AM
cron not working from crontab nor form /etc/cron/cron.d. What did SuSE change? JZL240I-U SUSE / openSUSE 11 01-04-2007 01:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration