区分退出超时和会话超时 [英] Differentiate between exit and session timeout
本文介绍了区分退出超时和会话超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下要求:
- 当bash会话被用户终止(退出)时,生成审核日志
- bash会话超时时生成审核日志
这些审核日志必须不同.我正在玩以下脚本trap.sh
:
Those audit logs must be different. I am playing around with the following script 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
它按预期工作.如果相反,我等待超时发生:
It works as expected. If instead, I wait for the timeout to happen:
valegon@precision ~ (master) $ bash
valegon@precision ~ (master) $ source trap.sh
valegon@precision ~ (master) $ timed out waiting for input: auto-logout
Exit
这里有两个问题:
- 超时正在触发退出,我不希望
- 我不知道该如何专门捕获超时
我该如何解决这些未解决的问题?
How can I solve these open issues?
推荐答案
第二次尝试
根据反馈,以前在EXIT上使用陷阱的解决方案效果不佳.或者,基于使用PROMPT_COMMAND似乎可以提供更好的里程.
Based on feedback, previous solution using trap on EXIT does not work well. Alternative, based on using PROMPT_COMMAND seems to give better mileage.
基本逻辑:
- 捕获命令提示符时间-开始)
- 在退出"事件中,检查(立即开始)> TMOUT
- 通常,退出,CTRL/D等操作将在1-2秒内完成.
#! /bin/bash
function pre_cmd {
START=$SECONDS
}
function log_exit {
if [ "$((SECONDS-START-TMOUT))" -ge 0 ] ; then
echo "TIMEOUT"
else
echo "Normal Exit"
fi
}
TMOUT=15
PROMPT_COMMAND=pre_cmd
trap 'log_exit' EXIT
这篇关于区分退出超时和会话超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文