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

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

问题描述

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

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.

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

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?

推荐答案

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

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

总是.

#   @(#)$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' 的链接,它看起来像:

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

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

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

'~/.cronfile' 是我的个人资料的一个变体,供 cron 使用 - 严格地非交互性并且为了嘈杂而没有回声.您可以安排执行 .profile 等.(REAL_HOME 内容是我的环境的人工制品 - 您可以假装它与 $HOME 相同.)

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.)

因此,此代码读取适当的环境,然后从我的主目录执行该命令的非 Cron 版本.因此,例如,我的工作日"命令如下所示:

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

每日"命令更简单:

:       "@(#)$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天全站免登陆