在运行 cronjobs 并使用来自 check_mk 的 mk-job 来监控其结果时,我偶然发现了这一点:
重击:
$ /usr/bin/time -f "time_exit: %x" timeout -s SIGKILL 2s sleep 10; echo "shell_exit: $?"
Command terminated by signal 9
time_exit: 0
shell_exit: 137
返回/usr/bin/time
的退出代码与它写入格式化输出的退出代码不同:
time_exit != shell_exit
为什么?
但是当使用默认的 SIGHUP 信号时,退出代码匹配:
$ /usr/bin/time -f "time_exit: %x" timeout -s SIGHUP 2s sleep 10; echo "shell_exit: $?"
Command exited with non-zero status 124
time_exit: 124
shell_exit: 124
timeout -k 10s 2s ...
同时,如果进程仍在运行,我将使用which 将首先发送 SIGHUP 并在 10 秒后发送 SIGKILL。希望 SIGHUP 能适当地阻止它。
背景
check_mk 提供 mk-job 来监控作业执行。mk-job 使用时间来记录执行时间和退出代码。
man time
:
time 命令在程序退出、停止或被信号终止时返回。如果程序正常退出,time的返回值就是它执行和测量的程序的返回值。否则,返回值为 128 加上导致程序停止或终止的信号编号。
man timeout
:
... 可能需要使用 KILL (9) 信号,因为无法捕获此信号,在这种情况下退出状态为 128+9 而不是 124。