![“脚本”会话日志记录让我退出两次](https://linux22.com/image/41229/%E2%80%9C%E8%84%9A%E6%9C%AC%E2%80%9D%E4%BC%9A%E8%AF%9D%E6%97%A5%E5%BF%97%E8%AE%B0%E5%BD%95%E8%AE%A9%E6%88%91%E9%80%80%E5%87%BA%E4%B8%A4%E6%AC%A1.png)
我正在使用描述的解决方案这里要记录我的 shell 会话,请将其添加到以下内容的末尾/etc/bash.bashrc
:
test "$(ps -ocommand= -p $PPID | awk '{print $1}')" == 'script' || (script -f /var/log/shellog/$USER-$(date -u +%Y.%m.%d-%H:%M:%S).${HOSTNAME:-$(hostname)}.$$.log)
这工作正常,但是,当我退出外壳程序时,我必须退出两次:一次从脚本退出,一次从外壳程序退出:
Last login: Wed Aug 6 12:43:29 2014 from *****
Script started, file is /var/log/shellog/camilstaps-2014.08.06-10:43:40.cs.localdomain.16048.log
camilstaps@cs:/$ exit
exit
Script done, file is /var/log/shellog/camilstaps-2014.08.06-10:43:40.cs.localdomain.16048.log
camilstaps@cs:/$ exit
exit
我发现这,并尝试相应地修改该行/etc/bash.bashrc
,但这不会改变任何内容:
test "$(ps -ocommand= -p $PPID | awk '{print $1}')" == 'script' || (script -f /var/log/shellog/$USER-$(date -u +%Y.%m.%d-%H:%M:%S).${HOSTNAME:-$(hostname)}.$$.log && exit)
我想这是因为我必须test
这么做,所以我还没有加入script
(没有它test
就会不断创造新的script
)。但我怎样才能让我不必退出两次呢?
答案1
按照要求。以下应该有效
[[ $(ps -ocommand= -p $PPID | awk '{print $1}') = script ]] || { script -f /var/log/shellog/$USER-$(date -u +%Y.%m.%d-%H:%M:%S).${HOSTNAME:-$(hostname)}.$$.log && exit ;}