8
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.

2 Answers 2

12

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to make it so you don't have to press enter to exit?
5

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.

I.e. instead of

$ python ./my_script.py

run:

$ exec python ./my_script.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.