goensch Posted May 26, 2024 Posted May 26, 2024 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 Quote
goensch Posted May 26, 2024 Author Posted May 26, 2024 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. Quote
MoneyBroz Posted May 26, 2024 Posted May 26, 2024 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. Quote
MoneyBroz Posted May 26, 2024 Posted May 26, 2024 Also you would need to have a way to stop your script. Krydos can explain more about this. Quote
goensch Posted May 26, 2024 Author Posted May 26, 2024 (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, 2024 by goensch Quote
goensch Posted May 26, 2024 Author Posted May 26, 2024 I think I have something to start with.... seems to be an issue with blanks in my code. ... Whitespaces... Quote
goensch Posted May 26, 2024 Author Posted May 26, 2024 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) Quote
wolstech Posted May 27, 2024 Posted May 27, 2024 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.