在运行命令之前让“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 版本。因此,例如,我的“工作日”命令如下所示:
: "@(#)$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