How to exit a Linux terminal using a Python script?

import sys def end(): foo=raw_input() sys.exit() print 'Press enter to Exit python and Terminal' end() 

When we run the program, we can exit Python Interpreter and Terminal. But it only leaves the python interpreter, not the terminal.

Thanks in advance.

+6
source share
2 answers

SIGHUP (hang up) will report the terminal output. The terminal must be your script parent process, therefore

 import os import signal os.kill(os.getppid(), signal.SIGHUP) 
+8
source

Instead of running the command from the shell with only the name of the command, run it with exec , which will force the shell to replace itself with the program. Then, when the program exits from the terminal window, it will also close.

those. instead

 $ python ./my_script.py 

mileage:

 $ exec python ./my_script.py 
+4
source

Source: https://habr.com/ru/post/1238799/


All Articles