bash Cron 作业不会获取 .bashrc 中设置的环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15557777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cron job does NOT get the environment variables set in .bashrc
提问by Peter Lee
Here is my cron job:
这是我的 cron 工作:
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?
在~/.bashrc
文件内部,我有export EDITOR=vim
,但在最终/tmp/cronjob.test
文件中,它仍然是空的?
So how can I get the environment variables (set in .bashrc
file) and use it in my cron job?
那么如何获取环境变量(在.bashrc
文件中设置)并在我的 cron 作业中使用它?
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
:
在/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
.
顺便说一句,该.bashrc
文件是 Ubuntu 12.04 附带的默认文件,但我添加了一行export EDITOR=vim
.
If I don't use the cron job, instead, just directly do this on the command line:
如果我不使用 cron 作业,则直接在命令行上执行此操作:
source .bashrc; echo $EDITOR # Output: vim
回答by Alex
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:
source ~/.bashrc
不工作的原因是您的内容~/.bashrc
(Ubuntu 12.04 中的默认内容)。如果您查看它,您将在第 5 行和第 6 行看到以下内容:
# 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'
:
PS1
变量是为交互式 shell 设置的,因此在运行 via 时它不存在cron
,即使您将它作为登录 shell 执行。生成的文件内容证实了这一点/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
:
为了使source ~/.bashrc
工作,注释掉检查PS1
变量存在的行~/.bashrc
:
#[ -z "$PS1" ] && return
This will make bash
execute the entire contents of ~/.bashrc
via cron
这会让bash
执行的全部内容~/.bashrc
通过cron
回答by vdua
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
@alex 提供的答案是正确的,但在 Ubuntu 13.10 中,代码已稍作修改。没有 $PS1 变量,但在第 6-9 行有一个代码
case $- in
*i*) ;;
*) return;;
esac
Just commenting out the line which returns works. i.e. the code below works
只需注释掉返回的行即可。即下面的代码有效
case $- in
*i*) ;;
# *) return;;
esac
回答by Peter Lee
I just tried a file .env_setup_rc
file with only one line export EDITOR=vim
, surprisingly it's working.
我刚刚尝试了一个.env_setup_rc
只有一行的文件export EDITOR=vim
,令人惊讶的是它正在工作。
So I guess there is something in .bashrc
conflicting with the cron job bash command.
所以我猜有一些东西.bashrc
与 cron job bash 命令相冲突。