Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Exit Linux terminal using 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 should able to exit the Python Interpreter and Terminal itself. But it only exits python interpreter, not the terminal.

Thanks in advance.

like image 381
Jerome Paulraj Avatar asked Dec 21 '15 05:12

Jerome Paulraj


1 Answers

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)
like image 150
tdelaney Avatar answered Oct 16 '22 19:10

tdelaney