在 SSH 退出时执行脚本

在 SSH 退出时执行脚本

退出 SSH 会话时是否可以执行 shell 脚本?我想在退出时执行脚本来禁用服务

答案1

如果你的 shell 是bash,一种方法可能是使用 中的命令~/.bash_logout。从man bash

When a login shell exits, bash reads and executes commands from the file 
~/.bash_logout, if it exists.

SSH 运行登录 shell,因此当它退出时.bash_logout应该运行。您可以通过测试变量的值来检查它是否是 SSH 会话SSH_TTY。因此您可以编辑(或创建)~/.bash_logout包含以下内容的内容,例如:

if [[ -n $SSH_TTY ]]
then
    stop your service
fi

相关内容