我希望我的脚本在运行结束后能够自动注销当前用户。这个脚本将在基于Linux的服务器(Ubuntu)上运行。我尝试过下面这行代码,但它不能正常工作。
subprocess.call(['logout'])
出现以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
我也尝试过:
subprocess.Popen(['sudo', 'logout'])
但是用户需要输入密码才能退出登录。如果没有sudo权限,会出现以下错误信息:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
我不想以root身份运行脚本。
我希望能够直接注销用户而无需输入密码。当您在常规终端使用logout时,通常无需输入密码,因此我真的不明白为什么这会成为一个问题。
非常感谢所有的帮助。
编辑:我已经通过向~/.bash_login添加一些额外的代码找到了解决我的问题的方法(请参见我的答案),但为什么上面尝试的方法没有得到答案呢?
sudo
吗?如果我尝试sudo logout
,会提示sudo: logout: command not found
。我认为logout
是一个 shell 内置命令... - glglglshell=True
调用(...),这样命令将被评估为在shell中输入的方式。 - Mikko Ohtamaa