2

My friend is in a macOS environment and he wanted to call os.system('exit') at the end of his python script to make the terminal close. It doesn't. This doesn't surprise me but I would like to know what exactly is going on between the python script and the terminal when this call is made.

In my mental simulation the terminal should have to tell you that there are still running jobs, but that doesn't happen either.

As a side question : will some less common terminals close when a process calls this?

1
  • 1
    os._exit() might work if you don't care about normal exit processing. Commented Sep 9, 2011 at 23:44

3 Answers 3

5

read the help:

Execute the command (a string) in a subshell.

A subshell is launched, and exit is run in that subshell.

To exit the enclosing terminal, you have to kill the parent. One way to do it is:

os.system("kill -9 %d"%(os.getppid())
Sign up to request clarification or add additional context in comments.

1 Comment

Just add a parenthesis: os.system("kill -9 %d"%(os.getppid()))
2

The system function starts another shell to execute a command. So in this case your Python scripts starts a shell and runs "exit" command in there, which makes that process exit. However, the Python script itself, including a terminal where it is running, continues to run. If the intent is to kill the terminal, you have to get the parent process ID and send a signal requesting it to stop. That will kill both Python script and a terminal.

Comments

1

Remember that system first spawns/forks a sub-shell to execute its commands. In effect, you are asking only the sub-shell to exit.

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.