Jump to content

[Answered] Kill Job with python


goensch

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...