Re: What is the correct way to set up login environment in crontab?

2011-11-09 Thread Greg Wooledge
On Tue, Nov 08, 2011 at 09:46:37PM -0600, Peng Yu wrote:
> I need to use cron to run some job. I know that cron only set up very
> basic environment. I'd like to duplicate my login environment.

Just source /etc/profile and your ~/.bash_profile or ~/.profile (or
whatever) from the script that your cron job executes.

Personally I would advise against this.  Login environments are for
interactive logins, not cron jobs.



Re: What is the correct way to set up login environment in crontab?

2011-11-09 Thread Peng Yu
On Wed, Nov 9, 2011 at 7:45 AM, Greg Wooledge  wrote:
> On Tue, Nov 08, 2011 at 09:46:37PM -0600, Peng Yu wrote:
>> I need to use cron to run some job. I know that cron only set up very
>> basic environment. I'd like to duplicate my login environment.
>
> Just source /etc/profile and your ~/.bash_profile or ~/.profile (or
> whatever) from the script that your cron job executes.
>
> Personally I would advise against this.  Login environments are for
> interactive logins, not cron jobs.

So neither -i nor -l is necessary?

-- 
Regards,
Peng



Re: What is the correct way to set up login environment in crontab?

2011-11-09 Thread Peng Yu
On Wed, Nov 9, 2011 at 7:45 AM, Greg Wooledge  wrote:
> On Tue, Nov 08, 2011 at 09:46:37PM -0600, Peng Yu wrote:
>> I need to use cron to run some job. I know that cron only set up very
>> basic environment. I'd like to duplicate my login environment.
>
> Just source /etc/profile and your ~/.bash_profile or ~/.profile (or
> whatever) from the script that your cron job executes.
>
> Personally I would advise against this.  Login environments are for
> interactive logins, not cron jobs.

I sourced my ~/.bashrc, which source some other files. It seems the
environment variables defined in these files are not seen with env.
Why is so?


-- 
Regards,
Peng



Re: What is the correct way to set up login environment in crontab?

2011-11-09 Thread Greg Wooledge
On Wed, Nov 09, 2011 at 10:29:52AM -0600, Peng Yu wrote:
> I sourced my ~/.bashrc, which source some other files. It seems the
> environment variables defined in these files are not seen with env.
> Why is so?

Without seeing the code?  Impossible to say.  But you're doing it backwards.
~/.bashrc should be sourced FROM ~/.bash_profile.  A login shell reads
~/.bash_profile only, so it's the responsibility of ~/.bash_profile to
read ~/.bashrc to set up aliases, functions, shopts, and other ephemeral
shell settings that can't be inherited from the environment.

http://mywiki.wooledge.org/DotFiles



Re: What is the correct way to set up login environment in crontab?

2011-11-09 Thread Peng Yu
On Wed, Nov 9, 2011 at 10:41 AM, Greg Wooledge  wrote:
> On Wed, Nov 09, 2011 at 10:29:52AM -0600, Peng Yu wrote:
>> I sourced my ~/.bashrc, which source some other files. It seems the
>> environment variables defined in these files are not seen with env.
>> Why is so?
>
> Without seeing the code?  Impossible to say.  But you're doing it backwards.
> ~/.bashrc should be sourced FROM ~/.bash_profile.  A login shell reads
> ~/.bash_profile only, so it's the responsibility of ~/.bash_profile to
> read ~/.bashrc to set up aliases, functions, shopts, and other ephemeral
> shell settings that can't be inherited from the environment.

Sorry for the confusion. ~/.bash_profile is not the problem here. I
have the following line in /path/programtorun.sh

. ~/.bashrc

In ~/.bashrc, I have ". ~/bash_some". In ~/.bash_some, I have some
variable assignment VAR=blah.

However, VAR is not seen in env in /path/programtorun.sh (called from cron).

-- 
Regards,
Peng



Re: What is the correct way to set up login environment in crontab?

2011-11-09 Thread Eric Blake

On 11/09/2011 10:14 AM, Peng Yu wrote:

variable assignment VAR=blah.


That sets up a bash-local variable.  If you want it to be exported to 
the environment visible to child processes, then you _also_ need to use 
export, as in either:


VAR=blah
export VAR

or

export VAR=blah



However, VAR is not seen in env in /path/programtorun.sh (called from cron).


Right - bash maintains two sets of variables, and only the exported ones 
are visible to children.


--
Eric Blake   ebl...@redhat.com+1-801-349-2682
Libvirt virtualization library http://libvirt.org