I want to know how I can stop the current current python script when it is already running.
To be clear, I want to write it in my own python script. So, the most important thing, in the main method, it checks if my python script is working or not. If it is running, I want to exit with an error message.
I am currently trying to use this code:
if os.system("ps ax | grep sample_python.py") == True: print "fail" exit(0) else: print "pass"
The above code looks like grepping the name of python .. but it always goes into else, instead of going into the if loop and exiting.
So, for testing, I run my script, and on the other terminal I run my script again. The first script does not stop, but the second python does not enter the if loop to print an error message.
How can i fix this?
Ok, I found a dumb thing, and I want to see if anyone can help me get around this. So I decided to kill python, which runs only if another python is currently running. but stupid thing .. since I run my python and run code verification inside this python .. it will always exit .. because as soon as I run python, the PID will be created and will be launched ...
So, python launch β pid is created and running β check if python is running β yes python is running β exit.
I want to see if there is a way to make python run β check if python is running or not β if it is running, kill the current python and not the one that was running.
python linux unix
Tim
source share