Search code examples
pythonprocessterminalexitterminate

os.system('exit') in python


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?


Solution

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