如何使用 Python 脚本退出 Linux 终端? [英] How to Exit Linux terminal using Python script?

查看:27
本文介绍了如何使用 Python 脚本退出 Linux 终端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import sys

def end():
    foo=raw_input()
    sys.exit()

print 'Press enter to Exit python and Terminal'

end()

当我们运行程序时,我们应该能够退出 Python 解释器和终端本身.但它只退出python解释器,而不是终端.

When we run the program, we should able to exit the Python Interpreter and Terminal itself. But it only exits python interpreter, not the terminal.

提前致谢.

推荐答案

SIGHUP(挂断)会告诉终端退出.终端应该是你脚本的父进程,所以

SIGHUP (hang up) will tell the terminal to exit. The terminal should be your script's parent process, so

import os
import signal
os.kill(os.getppid(), signal.SIGHUP)

这篇关于如何使用 Python 脚本退出 Linux 终端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆