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

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.