如何在ssh重启/暂停时注销用户,但保存了.bash_history?

当我通过SSH重新启动服务器时,会话只是挂起,而不是很好地注销,这样我就可以继续使用当前的terminal。 我尝试了以下来解决这个问题: https : //bbs.archlinux.org/viewtopic.php? id = 50089但是,我的bash历史没有保存之前重新启动。

有没有一种方法可以在重启/暂停命令之前很好地注销所有用户(以便保存他们的bash历史logging)? 即重新启动/暂停之前保存bash历史logging和最终用户会话?

操作系统:Ubuntu服务器11.04

您需要在每个用户会话中执行类似history -a的操作。 我不能想到一个“好”的方法来做到这一点,但在bash中使用PROMPT_COMMAND将会起作用,PROMPT_COMMAND是一个envvariables,用于每次bash返回到提示时执行的命令,也就是说,它会自动将历史刷新到.bash_history在每个命令执行。 工程,但可能不是最好的解决scheme。 把它放在.bashrc或profile.d文件中(如果你在profile.d兼容的发行版)。

  export PROMPT_COMMAND ='history -a' 

我find一个解决scheme:

我只需要使用“killall -u”而不是“skill -KILL -u”。 :)如这里所示: http : //www.cyberciti.biz/tips/howto-linux-kill-and-logout-users.html ,命令“技能”似乎已经过时,应该使用像pkill和killall这样的其他命令代替。 pkill没有做我想要的,但是killall做了。 🙂

所以这是完整的解决scheme:

1)创build一个包含以下内容的脚本/etc/init.d/killusers:

 #!/bin/bash # # chkconfig: 35 90 12 # description: Foo server # # Get function from functions library #. /etc/init.d/functions . /lib/lsb/init-functions # Start the service FOO start() { #initlog -c "echo -n Starting FOO server: " #who | cut -d " " -f1 | uniq | xargs killall -u #who | cut -d " " -f1 | uniq | xargs skill -KILL -u #success $"FOO server startup" echo "Do nothing" } # Restart the service FOO stop() { #initlog -c "echo -n Stopping FOO server: " who | cut -d " " -f1 | uniq | xargs killall -u #who | cut -d " " -f1 | uniq | xargs skill -KILL -u #who | cut -d " " -f1 | uniq | xargs pkill -STOP -u } ### main logic ### case "$1" in start) start ;; stop) stop ;; status) status FOO ;; restart|reload|condrestart) stop start ;; *) echo $"Usage: $0 {start|stop|restart|reload|status}" exit 1 esac exit 0 

(基于http://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html的脚本)

2)使其可执行:

 sudo chmod +x /etc/init.d/killusers 

3)将其添加到关机脚本中:

 sudo update-rc.d killusers defaults 

注1:我认为运行级别6应该足够了,但我默认只是为了确保。 如果脚本刚刚在closures期间运行,脚本可能也只能包含kill命令。

注2:要立即保存bash历史logging,可以使用(感谢HampusLi):

 history -a 

所以你也可以运行:

 history -a && sudo reboot 

但我希望它可以为任何用户工作,而不必创build一个别名,脚本或类似的东西。