goensch Posted May 26 Posted May 26 Hi, I have a scheduled task. Sometimes it does not run through, the next is started.... It adds up and eats memory. So I want to check for a previous insta ce running and kill it. This is my idea: print("test " ) from os import getpid from os.path import exists from psutil import pid_exists, Process PATH_PIDFILE = "../pid.txt" my_pid = getpid() if exists(PATH_PIDFILE): with open(PATH_PIDFILE) as f: pid = f.read() pid = int(pid) if pid.isnumeric() else None if pid is not None and pid_exists(pid) and Process(pid).cmdline() == Process(my_pid).cmdline(): # Exists # Kill p = psutil.Process(pid) p.terminate() #print("killed ") with open(PATH_PIDFILE, "w") as f: f.write(str(my_pid)) Howorever a soon as I cute getpid linee it doesn't run anymore
goensch Posted May 26 Author Posted May 26 Just now, goensch said: Hi, I have a scheduled task. Sometimes it does not run through, the next is started.... It adds up and eats memory. So I want to check for a previous insta ce running and kill it. This is my idea: print("test " ) from os import getpid from os.path import exists from psutil import pid_exists, Process PATH_PIDFILE = "../pid.txt" my_pid = getpid() if exists(PATH_PIDFILE): with open(PATH_PIDFILE) as f: pid = f.read() pid = int(pid) if pid.isnumeric() else None if pid is not None and pid_exists(pid) and Process(pid).cmdline() == Process(my_pid).cmdline(): # Exists # Kill p = psutil.Process(pid) p.terminate() #print("killed ") with open(PATH_PIDFILE, "w") as f: f.write(str(my_pid)) Howorever a soon as I cute getpid linee it doesn't run anymore Sorry. As soon as I include getpid it does not run.
MoneyBroz Posted May 26 Posted May 26 19 minutes ago, goensch said: Sorry. As soon as I include getpid it does not run. getpid isn't installed on any python version on tommy. Krydos can install it if necessary, just include what python version you are using and your username.
MoneyBroz Posted May 26 Posted May 26 Also you would need to have a way to stop your script. Krydos can explain more about this.
goensch Posted May 26 Author Posted May 26 (edited) Thanks it is 3.10 on Tommy. Regarding stopping, I want to use the above code. With psutil process terminate. I saw psutil is installed. But Krydos also told me is is always installed. I assumed getpid is part of os. The include looked like that.... But I do not really know Python. It is not the include line. It is as soon as I remove a # In front of the line using getpid. I out comment all lines and activated them top to bottom. Edited May 26 by goensch
goensch Posted May 26 Author Posted May 26 I think I have something to start with.... seems to be an issue with blanks in my code. ... Whitespaces...
goensch Posted May 26 Author Posted May 26 Now it works. It was the whitespaces.... I love languages with {} (or begin end) , that is a strange idea. Anyways, thanks to MoneyBroz and Krydos (in the Discord)
wolstech Posted May 27 Posted May 27 Yeah, Python is extremely picky about spaces (and line-endings as well) and is a major reason I can't stand the language...it uses spaces to define code blocks instead of the (much less problematic) curly braces { } used by every other language. Glad to see you got it working though
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now