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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:28:46  来源:igfitidea点击:

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

bashcronenvironment-variables

提问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 ~/.bashrcfile, I have export EDITOR=vim, but in the final /tmp/cronjob.testfile, it's still empty?

~/.bashrc文件内部,我有export EDITOR=vim,但在最终/tmp/cronjob.test文件中,它仍然是空的?

So how can I get the environment variables (set in .bashrcfile) 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 .bashrcfile 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 ~/.bashrcnot 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

PS1variable 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 ~/.bashrcwork, comment out the line that checks for presence of the PS1variable in ~/.bashrc:

为了使source ~/.bashrc工作,注释掉检查PS1变量存在的行~/.bashrc

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

This will make bashexecute the entire contents of ~/.bashrcvia 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_rcfile with only one line export EDITOR=vim, surprisingly it's working.

我刚刚尝试了一个.env_setup_rc只有一行的文件export EDITOR=vim,令人惊讶的是它正在工作。

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

所以我猜有一些东西.bashrc与 cron job bash 命令相冲突。