我有一个crontab每小时运行一次。运行它的用户在.bash_profile中有环境变量,当用户从终端运行作业时,这些环境变量是有效的,然而,显然这些环境变量在crontab运行时不会被拾取。

我已经尝试在.profile和.bashrc中设置它们,但它们似乎仍然没有被拾取。有人知道我可以把crontab可以获取的环境变量放在哪里吗?


当前回答

对于这个问题,我还有一个解决方案:

0 5 * * * . $HOME/.profile; /path/to/command/to/run

在这种情况下,它将选择$HOME/中定义的所有环境变量。概要文件。

当然$HOME也没有设置,你必须用$HOME的完整路径替换它。

其他回答

让'cron'运行一个shell脚本,在运行命令之前设置环境。

总是这样。

#   @(#)$Id: crontab,v 4.2 2007/09/17 02:41:00 jleffler Exp $
#   Crontab file for Home Directory for Jonathan Leffler (JL)
#-----------------------------------------------------------------------------
#Min     Hour    Day     Month   Weekday Command
#-----------------------------------------------------------------------------
0        *       *       *       *       /usr/bin/ksh /work1/jleffler/bin/Cron/hourly
1        1       *       *       *       /usr/bin/ksh /work1/jleffler/bin/Cron/daily
23       1       *       *       1-5     /usr/bin/ksh /work1/jleffler/bin/Cron/weekday
2        3       *       *       0       /usr/bin/ksh /work1/jleffler/bin/Cron/weekly
21       3       1       *       *       /usr/bin/ksh /work1/jleffler/bin/Cron/monthly

~/bin/Cron中的脚本都是指向单个脚本'runcron'的链接,它看起来像:

:       "$Id: runcron.sh,v 2.1 2001/02/27 00:53:22 jleffler Exp $"
#
#       Commands to be performed by Cron (no debugging options)

#       Set environment -- not done by cron (usually switches HOME)
. $HOME/.cronfile

base=`basename $0`
cmd=${REAL_HOME:-/real/home}/bin/$base

if [ ! -x $cmd ]
then cmd=${HOME}/bin/$base
fi

exec $cmd ${@:+"$@"}

(使用较旧的编码标准编写-现在,我会使用shebang '#!开头说。)

“~ /。Cronfile '是我个人资料的一个变种,供cron使用——严格地说,没有交互,也没有为了吵闹而产生的回声。您可以安排执行.profile等等。(REAL_HOME的东西是我的环境的一个人工制品-你可以假装它是相同的$HOME。)

因此,这段代码读取适当的环境,然后从我的主目录执行非cron版本的命令。例如,我的'weekday'命令是这样的:

:       "@(#)$Id: weekday.sh,v 1.10 2007/09/17 02:42:03 jleffler Exp $"
#
#       Commands to be done each weekday

# Update ICSCOPE
n.updics

'daily'命令更简单:

:       "@(#)$Id: daily.sh,v 1.5 1997/06/02 22:04:21 johnl Exp $"
#
#       Commands to be done daily

# Nothing -- most things are done on weekdays only

exit 0

从命令行运行crontab -e时,可以在crontab本身中定义环境变量。

LANG=nb_NO.UTF-8
LC_ALL=nb_NO.UTF-8
# m h  dom mon dow   command

* * * * * sleep 5s && echo "yo"

此特性仅适用于特定的cron实现。Ubuntu和Debian目前使用vixie-cron,它允许在crontab文件中声明这些(也可以是GNU mcron)。

Archlinux和RedHat使用cronie,它不允许声明环境变量,并且会在cron.log中抛出语法错误。每个条目都可以采取变通措施:

# m h  dom mon dow   command
* * * * * export LC_ALL=nb_NO.UTF-8; sleep 5s && echo "yo"

我尝试了大部分提供的解决方案,但一开始都没用。然而,事实证明,并不是解决方案失败了。显然,我的~/。Bashrc文件以以下代码块开始:

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

This basically is a case statement that checks the current set of options in the current shell to determine that the shell is running interactively. If the shell happens to be running interactively, then it moves on to sourcing the ~/.bashrc file. However, in a shell invoked by cron, the $- variable doesn't contain the i value which indicates interactivity. Therefore, the ~/.bashrc file never gets sourced fully. As a result, the environment variables never got set. If this happens to be your issue, feel free to comment out the block of code as follows and try again:

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

我希望这对你有用

对我有用的(基于debian):

创建一个包含所有需要的env变量的文件: # !/bin/bash env | grep VAR1= > /etc/environment . env | grep VAR1= > /etc/environment . env | /etc/environment . env | grep VAR2= >> /etc/environment . env | grep VAR3= >> 然后,通过在调用需要它的脚本之前调用env文件来构建crontab内容,从而启动cron服务 (crontab -l;Echo '* * * * *。/etc/environment;/usr/local/bin/python /mycode.py >> /var/log/cron-1.log 2>&1') | crontab 服务cron启动

注意:对于python用例,一定要调用整个python路径,否则可能会调用错误的python,产生无意义的语法错误

另一种方式-受此启发-“注入”变量如下(fcron的例子):

%daily 00 12 \
    set -a; \
    . /path/to/file/containing/vars; \
    set +a; \
    /path/to/script/using/vars

从帮助集:

-a标记已修改或创建用于导出的变量。 使用+而不是-会导致这些标志被关闭。

因此,set -和set +之间的所有内容都被导出到env中,然后可用于其他脚本,等等。如果不使用set,变量将只存在于set中。

除此之外,当程序需要一个非根帐户来运行,但你需要在其他用户的环境中使用一些变量时,传递变量也很有用。下面是一个传入nullmailer vars来格式化电子邮件头的例子:

su -s /bin/bash -c "set -a; \
                    . /path/to/nullmailer-vars; \
                    set +a; \
                    /usr/sbin/logcheck" logcheck