0

Is it possible in Python to close a script using just the filename of the script? I have two scripts which don't work at the same time so on startup I want one of the scripts to end the other script. Is this possible? If so, please let me know how.

Any help greatly appreciated. Thanks,

Mitra0000

Mitra0000
  • 172
  • 1
  • 1
  • 10
  • Possible duplicate of [Terminate a python script from another python script](http://stackoverflow.com/questions/3054740/terminate-a-python-script-from-another-python-script) – 2rs2ts Dec 29 '16 at 21:33

1 Answers1

0

In this answer, it says that you can. You would need a "PID (Process Identifier)" and know whether you are using Unix or Python. Why? Because there are two different ways:

Unix:

import os, signal
os.kill(5383, signal.SIGKILL)

Windows:

import subprocess as s
def killProcess(pid):
    s.Popen('taskkill /F /PID {0}'.format(pid), shell=True)

The answer also mentioned:

You can send the PID to the other programm or you could search in the process-list to find the name of the other script and kill it with the above script.

Community
  • 1
  • 1
Anthony Pham
  • 3,096
  • 5
  • 29
  • 38
  • Thank you for the answer. Now, when the first program runs it saves its PID to an external json file. The second program, when run, opens the json file and kills the PID stored. Whenever the first program is run, the PID is updated and whenever the second program runs the first is closed. Thank you for your help. – Mitra0000 Dec 29 '16 at 22:04