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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…