I want to know how I can stop my current python script when it is already running.
To be clear, I want to write it in my own python script. So the very first thing, in main method, it will check if my python script is already running or not. If it is running, I want to exit out with an error message.
Currently I am trying this code:
if os.system("ps ax | grep sample_python.py") == True:
print "fail"
exit(0)
else:
print "pass"
The above code looks like it is grepping the python name.. but it always go to else loop instead of going into the if loop and exit..
So for testing purposes, I run my script and on another terminal I run my script again. First script doesn't stop but 2nd python doesn't go into the if loop to print out error message.
How can I fix this?
Okay I found a dumb thing and I want to see if someone can help me go around it.. So my point was to kill the python that is just started if another python is currently running.. but the dumb thing is.. since I am running my python and running a checking coding inside of this python.. it will always exit.. because as soon as I start the python PID will be created and will be running...
So.. python starts -> pid created and running -> check if python is running -> yes python is running -> exit.
I want to see if there is a way to do python starts -> check if python is already running or not -> if it is running kill the current python not the one that was running.
ps ax ...both when your script is running and when not to check if you have the right command line. Then you write a python script which just prints the result of theos.system( ...call, again in both cases to check that there is a difference. Then you should be able to write the script you need.pgrepandpkillto find and kill processes without including the grep process itself (there are also various shell utilities to ensure only one copy of something is running, though they might not be appropriate for your situation)ps | grep progname, one of the processes that gets listed is grep command. And grep's command line parameter "progname" is included. This might be OS dependent.