由于这只是为了好玩,所以这里有一个工作:如果用户在一段时间内忽略提示,或者关闭终端/终止进程,则让脚本将用户注销。以下是 gnome 的样子:
import os
def set_exit_handler(func):
import signal
signal.signal(signal.SIGHUP, func) # on closing the terminal
signal.signal(signal.SIGTERM, func) # on killing the process
import atexit
atexit.register(func) # on Ctrl-C, Ctrl-D and other proper exits
if __name__ == "__main__":
def on_exit(a=None, b=None):
print "exit handler triggered"
os.system("gnome-session-quit --logout --no-prompt")
set_exit_handler(on_exit)
print "Enter password:"
raw_input()
# ... some verification/timeout code ...
如果用户现在将关闭终端或终止进程,他将立即注销:)