退出ssh会话时如何执行脚本?

时间:2017-04-14 03:01:18

标签: ssh

.ssh / rc可以在用户登录时执行脚本 但是当用户使用ssh注销时,它是如何执行脚本的?

1 个答案:

答案 0 :(得分:1)

您可以在用户注销时添加陷阱来执行脚本:

Traceback (most recent call last):
File "/Users/zhangchaont/python/Course Python Programming/6.7V2.py", line 122, in <module>
    main()
File "/Users/zhangchaont/python/Course Python Programming/6.7V2.py", line 21, in main
    for line in txtFile:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 33: ordinal not in range(128)

如果shell是bash,那么放在trap 'bash /path/to/yourscript.sh; exit' 0 文件中的任何代码都将在注销时执行。对于csh,该文件是.logout。

另一种选择是运行这样的守护进程:

~/.bash_logout

用户注销后,可以随时执行您的脚本。

相关问题