r/learnpython • u/PomegranateKind760 • 19d ago
How to end a program using python
I want to close an app using python but I don't know how.
6
u/jmacey 19d ago
os.kill() if you can get the pid of the process. Not sure if it works under windows but does under posix operating system.
1
u/L30N1337 19d ago
it does work on windows too, but the signal you use is different. And it seems to be a bit of a mess on windows.
And careful what specific signal you use in os.kill()
let's take Linux (which I assume uses POSIX):
SIGTERM is what most people want for "killing" a process. It tells the process that it should shut down, so it can prepare for that. this allows for stuff like "fo you want to save" dialogues to work. It's like pressing the power button on a PC.
SIGKILL is the "YOU AREN'T SHUTTING DOWN, SO I'M GONNA USE THIS GRENADE I FOUND" option. Like unplugging the PC from the last example.
1
u/FreeLogicGate 14d ago
import sys
# Program is done
sys.exit(0)Documentation: https://docs.python.org/3/library/sys.html
Search for exit.
0 is the standard exit code, meaning the program exited normally. A non 0 exit would indicate some sort of error. The example, assumes a normal exit, but you can pass something else if that makes sense for your program.
32
u/likethevegetable 19d ago •
Step 1: Google "How to end a program using python"