如何从python代码退出到linux登录界面?

时间:2017-07-23 03:35:54

标签: python shell login-control

假设我使用admin和密码登录linux系统。 如何使用python代码注销admin用户。?

我试过以下:

        String name = AuthenticationUtil.getFullyAuthenticatedUser();
        System.out.println("Current user:"+name);
        PersonService personService=serviceRegistry.getPersonService();
        NodeRef node=personService.getPerson(name);
        NodeService nodeService=serviceRegistry.getNodeService();
        Map<QName, Serializable> props=nodeService.getProperties(node);

        for (Entry<QName, Serializable> entry : props.entrySet())
        {
            System.out.println(entry.getKey() + "/" + entry.getValue());
        }

这不起作用..

1 个答案:

答案 0 :(得分:0)

必须找到连接到终端的会话负责人。然后向此进程发送挂断信号。如果它不起作用,发送一个终止信号。

import os
import signal
import subprocess
import time

def runcmd(*args):
    cmd = subprocess.Popen(args, stdout=subprocess.PIPE, universal_newlines=True)
    return cmd.communicate()[0]

tty = runcmd('tty').strip()
for line in runcmd('ps', '-o', 'pid,sess', '-t', tty).split('\n'):
    if line:
        pid, sess = line.split()
        if pid == sess:
            os.kill(int(pid), signal.SIGHUP)
            time.sleep(5.0)
            os.kill(int(pid), signal.SIGKILL)