os.system('exit') in python

10,587

Solution 1

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())

Solution 2

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

Share:
10,587
Vasiliy Sharapov
Author by

Vasiliy Sharapov

How do I set logelevel to DEBUG on this thing?

Updated on June 04, 2022

Comments

  • Vasiliy Sharapov
    Vasiliy Sharapov almost 2 years

    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?