【发布时间】:2020-02-18 12:34:34
【问题描述】:
我有以下要求:
- 当用户终止 bash 会话时生成审核日志(退出)
- 当 bash 会话超时时生成审核日志
这些审核日志必须不同。我正在玩以下脚本trap.sh:
export TMOUT=10
function handle-timeout {
echo "Timeout"
}
function handle-exit {
echo "Exit"
}
trap handle-exit EXIT
现在如果我这样做:
valegon@precision ~ (master) $ bash
valegon@precision ~ (master) $ source trap.sh
valegon@precision ~ (master) $ exit
Exit
它按预期工作。相反,我等待超时发生:
valegon@precision ~ (master) $ bash
valegon@precision ~ (master) $ source trap.sh
valegon@precision ~ (master) $ timed out waiting for input: auto-logout
Exit
这里有两个问题:
- 超时触发EXIT,我不希望这样
- 我不知道如何专门捕获超时
如何解决这些未解决的问题?
【问题讨论】:
-
如果没有调用过程的合作,我认为这是不可行的。 Bash 没有超时;它是由启动 Bash 的进程处理的(我猜是 SSH,虽然我可能忽略了其他机制……如果你从 1980 年代开始在这里旅行,请使用 Telnet)。
-
@tripleee。 Bash 有超时,来自手册:
TMOUT If set to a value greater than zero, TMOUT is treated as the default timeout for the read builtin. The select command terminates if input does not arrive after TMOUT seconds when input is coming from a terminal. In an interactive shell, the value is interpreted as the number of seconds to wait for a line of input after issuing the primary prompt. Bash terminates after waiting for that number of seconds if a complete line of input does not arrive. -
是的,没错,但我仍然相信您实际上在运行 Bash 的父进程中的会话层遇到了超时。
-
@tripleee:一点也不。没有采购
traph.sh不会发生超时。不涉及父进程。 -
我查看了code in
eval.c,它似乎并没有以任何方式区分exit事件。它在内部使用SIGALRM,但trap '...' ALRM对我没有任何用处(MacOS 上的 Bash 3.x)。