如何使用Python脚本退出Linux终端?

时间:2021-03-04 17:31:14
import sys

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

print 'Press enter to Exit python and Terminal'

end()

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.

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

Thanks in advance.

提前致谢。

2 个解决方案

#1


8  

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

SIGHUP(挂机)会告诉终端退出。终端应该是您脚本的父进程,所以

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

#2


3  

Instead of running the command from the shell with just the command name, run it with exec which will cause the shell to replace itself with the program. Then when the program exits the terminal window will close as well.

而不是仅使用命令名从shell运行命令,而是使用exec运行它,这将导致shell将程序替换为自身。然后当程序退出终端窗口时也会关闭。

I.e. instead of

即代替

$ python ./my_script.py

run:

$ exec python ./my_script.py

#1


8  

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

SIGHUP(挂机)会告诉终端退出。终端应该是您脚本的父进程,所以

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

#2


3  

Instead of running the command from the shell with just the command name, run it with exec which will cause the shell to replace itself with the program. Then when the program exits the terminal window will close as well.

而不是仅使用命令名从shell运行命令,而是使用exec运行它,这将导致shell将程序替换为自身。然后当程序退出终端窗口时也会关闭。

I.e. instead of

即代替

$ python ./my_script.py

run:

$ exec python ./my_script.py