Jump to content

Recommended Posts

Posted

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

Posted
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.

Posted
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.

Posted

Also you would need to have a way to stop your script. Krydos can explain more about this.

Posted (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 by goensch
Posted

I think I have something to start with....  seems to be an issue with blanks in my code.  ...  Whitespaces...

Posted

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)

Posted

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 :) 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...