Okay, so I'm having a little trouble. Let's say I run "scriptA.py" and it has a GUI (Tkinter) and it is running in its mainloop. I want to create a "scriptB.py" that can end "scriptA.py"; would there be a way to do this regardless of the OS it is on (as in no Windows-only or Linux-only solutions please)?

Thanks in advance.

I have a small collection of receipes found on the web a long time ago. See if one of them can help you

# linux, mac
os.kill(pid, signal.SIGKILL)
killedpid, stat = os.waitpid(pid, os.WNOHANG)
if killedpid == 0:
print >> sys.stderr, "ACK! PROCESS NOT KILLED?"

# windows
handle = subprocess.Popen("someprocess here", shell=False)
subprocess.Popen("taskkill /F /T /PID %i"%handle.pid , shell=True)

# also
# Create a process that won't end on its own
import subprocess
process = subprocess.Popen(['python.exe', '-c', 'while 1: pass'])

# Kill the process using pywin32
import win32api
win32api.TerminateProcess(int(process._handle), -1)

# Kill the process using ctypes
import ctypes
ctypes.windll.kernel32.TerminateProcess(int(process._handle), -1)

# Kill the proces using pywin32 and pid
import win32api
handle = win32api.OpenProcess(PROCESS_TERMINATE, False, process.pid)
win32api.TerminateProcess(handle, -1)

# Kill the proces using ctypes and pid
import ctypes
handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, process.pid)
ctypes.windll.kernel32.TerminateProcess(handle, -1)

Okay, well after some testing around I got the first one to work flawlessly. I'm only going to be running the Windows version of my script through Cygwin, so I'm not sure if that supports the same PID system as Linux, but I'll post back when I know more.

Thank you so much!

Okay, well I just got it working on Windows also. I have discovered that I can get the process id the same way as in Linux, os.getpid(). I used the following piece of code to actually end the process:

subprocess.Popen("taskkill /F /T /PID %i"% pid , shell=True)
This question has already been answered. Start a new discussion instead.