[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: cronjob in user environment

On Tue, Sep 02, 2008 at 02:18:45PM +0200, Steve S wrote:
> Hi all,
> I want to run a cronjob in a user's environment (i.e. as this user and
> not as root).
> The script foo.sh to be run by cron on behalf of the a user (johndoe)
> needs some env vars (paths) from that user's ~/.bashrc. 
> I read about several solutions:
> 1) use su
> If I try in the crontab
>     10 12 * * */2   su -p - johndoe -c /home/johndoe/foo.sh
> I get the message "su must be run from a terminal" in the cron mail.

I would use 'crontab -e' as the user to define the cronjob.  Then you
do not have to specify the user.

> 2) define env vars in the crontab
> It works if I define the env vars as a full path
>     $SOME_VAR=/full/path/1
>     $SOME_OTHER_VAR=/full/path/2
>     10 12 * * */2   su -p - johndoe -c /home/johndoe/foo.sh
> In ~/.bashrc, I have 
>     $SOME_VAR=$HOME/path/1
>     $SOME_OTHER_VAR=$HOME/path/2
> and I'd rather not dublicate the definitions.

Just put early in you script:

source ~/.bashrc


Johann Spies          Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

     "But God said unto him, Thou fool, this night thy soul
      shall be required of thee: then whose shall those things
      be, which thou hast provided? So is he that layeth up
      treasure for himself, and is not rich toward God."
                                        Luke 12:20,21 

Reply to: