在哪里可以设置crontab会使用的环境变量? [英] Where can I set environment variables that crontab will use?

查看:275
本文介绍了在哪里可以设置crontab会使用的环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每个小时都有一个crontab。运行它的用户在用户从终端运行作业时起作用的 .bash_profile 中具有环境,然而,显然这些不被crontab拾取运行。



我已经尝试在 .profile .bashrc 但他们似乎还没有收拾起来。有没有人知道我可以把crontab的环境变量放在哪里?

解决方案

让'cron'运行一个shell脚本,运行命令前的环境。



始终。

 #@ (#)$ Id:crontab,v 4.2 2007/09/17 02:41:00 jleffler Exp $ 
#Jonathan Leffler(JL)主目录的Crontab文件
#------ -------------------------------------------------- ---------------------
#Min小时日月工作日命令
#------------- -------------------------------------------------- --------------
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 $

#由Cron执行的命令(无调试选项)

#设置环境 - 不是由cron(通常切换HOME)
。 $ HOME / .cronfile

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

if [ ! -x $ cmd]
然后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 $

#每个工作日要完成的命令

#更新ICSCOPE
n.updics

每日命令更简单:

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

#每天要完成的命令

#没有 - 大多数事情只在工作日完成

退出0


I have a crontab running every hour. The user running it has environment variabless in the .bash_profile that work when the user runs the job from the terminal, however, obviously these don't get picked up by crontab when it runs.

I've tried setting them in .profile and .bashrc but they still don't seem to get picked up. Does anyone know where I can put environment vars that crontab can pick up?

解决方案

Have 'cron' run a shell script that sets the environment before running the command.

Always.

#   @(#)$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

The scripts in ~/bin/Cron are all links to a single script, 'runcron', which looks like:

:       "$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 ${@:+"$@"}

(Written using an older coding standard - nowadays, I'd use a shebang '#!' at the start.)

The '~/.cronfile' is a variation on my profile for use by cron - rigorously non-interactive and no echoing for the sake of being noisy. You could arrange to execute the .profile and so on instead. (The REAL_HOME stuff is an artefact of my environment - you can pretend it is the same as $HOME.)

So, this code reads the appropriate environment and then executes the non-Cron version of the command from my home directory. So, for example, my 'weekday' command looks like:

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

# Update ICSCOPE
n.updics

The 'daily' command is simpler:

:       "@(#)$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会使用的环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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