crontab的工作没有得到环境变量设置在.bashrc文件 [英] Crontab Job Does NOT Get the Environment Variables Set in .bashrc File

查看:349
本文介绍了crontab的工作没有得到环境变量设置在.bashrc文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的cron作业:

  plee @龙:〜$ crontab的-l
* * * * * /斌/ bash的-l -c'源的〜/ .bashrc;回声$ EDITOR> /tmp/cronjob.test

和里面的〜/ .bashrc中文件,我有出口EDITOR = vim的,但在最后 /tmp/cronjob.test 文件,它仍然是空的?

所以,我怎么能得到环境变量(在的.bashrc 设置文件),并在我的cron作业使用它吗?

  plee @龙:〜$ lsb_release -a
没有LSB模块可供选择。
经销商ID:Ubuntu的
说明:Ubuntu的LTS 12.04
稿:12.04
codeNAME:precise
plee @龙:〜$的uname -a
Linux的龙3.2.0-26-仿制PAE#41 Ubuntu的SMP周四6月14日16时45分14秒UTC 2012 i686的的i686 i386的GNU / Linux的

如果使用:

  * * * * * /斌/ bash的-l -c -x'源的〜/ .bashrc;回声$ EDITOR> /tmp/cronjob.test'2  - ; /tmp/cron.debug.res

/tmp/cron.debug.res

  ...
++返回0
+源/home/plee/.bashrc
++ ['-z''']'
++回报
+回声

顺便说一句,在的.bashrc 文件与Ubuntu 12.04附带的默认之一,我增加了一个行出口EDITOR = vim的异常

如果我不使用cron作业,相反,只需直接做在命令行上:

 源的.bashrc;回声$ EDITOR#输出:VIM


解决方案

其原因源的〜/ .bashrc 不工作是在你的〜/ .bashrc中(默认的从Ubuntu 12.04)。如果你在看它,你会看到线5和6以下内容:

 #如果没有交互的运行,不做任何事
[-z$ PS1]&放大器;&安培;返回

PS1 变量设置为一个交互的shell,所以当通过的cron 运行它的缺席,即使你是执行它作为登录shell。这是由生成的文件/ bin中的内容,确认/ bash的-l -c -x'源的〜/ .bashrc;回声$ EDITOR> /tmp/cronjob.test

  +源/home/plee/.bashrc
++ ['-z''']'
++回报

若要源的〜/ .bashrc 工作,注释掉检查的 PS1的presence行变量〜/ .bashrc中

 #[-z$ PS1]&放大器;&安培;返回

这将使庆典通过 cron的执行〜/ .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

解决方案

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

这篇关于crontab的工作没有得到环境变量设置在.bashrc文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆