有点超出你的问题范围......但这就是我所做的。
“我如何测试 cron 作业?”问题与“如何测试在其他程序启动的非交互式上下文中运行的脚本?”密切相关。在 cron 中,触发器是一些时间条件,但许多其他 *nix 工具以非交互方式启动脚本或脚本片段,并且这些脚本运行的条件通常包含意外的内容并导致损坏,直到错误被解决。 (另见:https://stackoverflow.com/a/17805088/237059)
解决这个问题的一般方法很有帮助。
我最喜欢的技术之一是使用我编写的名为“crontest”的脚本。它在 cron 中的 GNU 屏幕会话中启动目标命令,这样您就可以连接一个单独的终端以查看发生了什么,与脚本交互,甚至使用调试器。
要进行设置,您可以在 crontab 条目中使用“all stars”,并将 crontest 指定为命令行上的第一个命令,例如:
* * * * * crontest /command/to/be/tested --param1 --param2
所以现在 cron 将每分钟运行一次您的命令,但 crontest 将确保一次只运行一个实例。如果命令需要时间运行,您可以执行“screen -x”来附加并观察它运行。如果命令是脚本,你可以在顶部放一个“读取”命令让它停止并等待屏幕附件完成(附加后按回车)
如果你的命令是一个 bash 脚本,你可以这样做:
* * * * * crontest --bashdb /command/to/be/tested --param1 --param2
现在,如果您使用“screen -x”附加,您将面临一个交互式 bashdb 会话,您可以单步执行代码、检查变量等。
#!/bin/bash
# crontest
# See https://github.com/Stabledog/crontest for canonical source.
# Test wrapper for cron tasks. The suggested use is:
#
# 1. When adding your cron job, use all 5 stars to make it run every minute
# 2. Wrap the command in crontest
#
#
# Example:
#
# $ crontab -e
# * * * * * /usr/local/bin/crontest $HOME/bin/my-new-script --myparams
#
# Now, cron will run your job every minute, but crontest will only allow one
# instance to run at a time.
#
# crontest always wraps the command in "screen -d -m" if possible, so you can
# use "screen -x" to attach and interact with the job.
#
# If --bashdb is used, the command line will be passed to bashdb. Thus you
# can attach with "screen -x" and debug the remaining command in context.
#
# NOTES:
# - crontest can be used in other contexts, it doesn't have to be a cron job.
# Any place where commands are invoked without an interactive terminal and
# may need to be debugged.
#
# - crontest writes its own stuff to /tmp/crontest.log
#
# - If GNU screen isn't available, neither is --bashdb
#
crontestLog=/tmp/crontest.log
lockfile=$(if [[ -d /var/lock ]]; then echo /var/lock/crontest.lock; else echo /tmp/crontest.lock; fi )
useBashdb=false
useScreen=$( if which screen &>/dev/null; then echo true; else echo false; fi )
innerArgs="$@"
screenBin=$(which screen 2>/dev/null)
function errExit {
echo "[-err-] $@" | tee -a $crontestLog >&2
}
function log {
echo "[-stat-] $@" >> $crontestLog
}
function parseArgs {
while [[ ! -z $1 ]]; do
case $1 in
--bashdb)
if ! $useScreen; then
errExit "--bashdb invalid in crontest because GNU screen not installed"
fi
if ! which bashdb &>/dev/null; then
errExit "--bashdb invalid in crontest: no bashdb on the PATH"
fi
useBashdb=true
;;
--)
shift
innerArgs="$@"
return 0
;;
*)
innerArgs="$@"
return 0
;;
esac
shift
done
}
if [[ -z $sourceMe ]]; then
# Lock the lockfile (no, we do not wish to follow the standard
# advice of wrapping this in a subshell!)
exec 9>$lockfile
flock -n 9 || exit 1
# Zap any old log data:
[[ -f $crontestLog ]] && rm -f $crontestLog
parseArgs "$@"
log "crontest starting at $(date)"
log "Raw command line: $@"
log "Inner args: $@"
log "screenBin: $screenBin"
log "useBashdb: $( if $useBashdb; then echo YES; else echo no; fi )"
log "useScreen: $( if $useScreen; then echo YES; else echo no; fi )"
# Were building a command line.
cmdline=""
# If screen is available, put the task inside a pseudo-terminal
# owned by screen. That allows the developer to do a "screen -x" to
# interact with the running command:
if $useScreen; then
cmdline="$screenBin -D -m "
fi
# If bashdb is installed and --bashdb is specified on the command line,
# pass the command to bashdb. This allows the developer to do a "screen -x" to
# interactively debug a bash shell script:
if $useBashdb; then
cmdline="$cmdline $(which bashdb) "
fi
# Finally, append the target command and params:
cmdline="$cmdline $innerArgs"
log "cmdline: $cmdline"
# And run the whole schlock:
$cmdline
res=$?
log "Command result: $res"
echo "[-result-] $(if [[ $res -eq 0 ]]; then echo ok; else echo fail; fi)" >> $crontestLog
# Release the lock:
9<&-
fi