Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cron job does NOT get the environment variables set in .bashrc

Here is my cron job:

plee@dragon:~$ crontab -l
* * * * * /bin/bash -l -c 'source ~/.bashrc; echo $EDITOR > /tmp/cronjob.test'

and inside ~/.bashrc file, I have export EDITOR=vim, but in the final /tmp/cronjob.test file, it's still empty?

So how can I get the environment variables (set in .bashrc file) and use it in my cron job?

plee@dragon:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 12.04 LTS
Release:        12.04
Codename:       precise
plee@dragon:~$ uname -a
Linux dragon 3.2.0-26-generic-pae #41-Ubuntu SMP Thu Jun 14 16:45:14 UTC 2012 i686 i686 i386 GNU/Linux

If use this:

* * * * * /bin/bash -l -c -x 'source ~/.bashrc; echo $EDITOR > /tmp/cronjob.test' 2> /tmp/cron.debug.res

In /tmp/cron.debug.res:

...
++ return 0
+ source /home/plee/.bashrc
++ '[' -z '' ']'
++ return
+ echo

BTW, the .bashrc file is the default one came with Ubuntu 12.04, with the exception that I added one line export EDITOR=vim.

If I don't use the cron job, instead, just directly do this on the command line:

source .bashrc; echo $EDITOR # Output: vim
like image 891
Peter Lee Avatar asked Mar 21 '13 20:03

Peter Lee


People also ask

Does crontab have access to environment variables?

You can define environment variables in the crontab itself when running crontab -e from the command line.

Does cron read bashrc?

Bookmark this question. Show activity on this post. All we know that cron ignores variables defined in “.

How do I set environment variables in bashrc?

In order to set a permanent environment variable in Bash, you have to use the export command and add it either to your “. bashrc” file (if this variable is only for you) or to the /etc/environment file if you want all users to have this environment variable.

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .


3 Answers

The reason for source ~/.bashrc not working is the contents on your ~/.bashrc (default one from Ubuntu 12.04). If you look in it you will see on lines 5 and 6 the following:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

PS1 variable is set for an interactive shell, so it's absent when run via cron, even though you are executing it as a login shell. This is confirmed by contents of the file produced by /bin/bash -l -c -x 'source ~/.bashrc; echo $EDITOR > /tmp/cronjob.test':

+ source /home/plee/.bashrc
++ '[' -z '' ']'
++ return

To make source ~/.bashrc work, comment out the line that checks for presence of the PS1 variable in ~/.bashrc:

#[ -z "$PS1" ] && return

This will make bash execute the entire contents of ~/.bashrc via cron

like image 193
Alex Avatar answered Oct 05 '22 01:10

Alex


Answer provided by @alex is correct but in Ubuntu 13.10 the code has been modified a little. There is no $PS1 variable but in lines 6-9 there is a code

case $- in 
   *i*) ;;       
   *) return;; 
esac

Just commenting out the line which returns works. i.e. the code below works

case $- in 
   *i*) ;;       
#   *) return;; 
esac
like image 26
vdua Avatar answered Oct 05 '22 01:10

vdua


I just tried a file .env_setup_rc file with only one line export EDITOR=vim, surprisingly it's working.

So I guess there is something in .bashrc conflicting with the cron job bash command.

like image 24
Peter Lee Avatar answered Oct 05 '22 01:10

Peter Lee