Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait until a certain process (knowing the "pid") end

I have this:

def get_process():
    pids = []
    process = None
    for i in os.listdir('/proc'):
        if i.isdigit():
            pids.append(i)

    for pid in pids:
        proc = open(os.path.join('/proc', pid, 'cmdline'), 'r').readline()
        if proc == "Something":
            process = pid

    return process          

def is_running(pid):
    return os.path.exists("/proc/%s" % str(pid))

Then i do this:

process = get_process()
if process == None:
    #do something
else:
    #Wait until the process end
    while is_running(process):
        pass

I think this is not the best way to wait for the process to terminate, there must be some function wait or something, but i can't find it.

Disclaimer: The process is not a child process

like image 318
Niko Avatar asked Oct 04 '11 19:10

Niko


People also ask

How do you wait for a process to finish in Linux?

Using a special variable($!) to find the PID(process ID) for that particular process. Print the process ID. Using wait command with process ID as an argument to wait until the process finishes. After the process is finished printing process ID with its exit status.

How do you check if a process is running using PID?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.

How do I find the PID of a process?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column. Click on any column name to sort.

How do you wait until something happens Python?

Though there are a plethora of ways to make a pause in Python the most prevalent way is to use the wait() function. The wait() method in Python is used to make a running process wait for another function to complete its execution, such as a child process, before having to return to the parent class or event.


3 Answers

I'm not really a Python programmer, but apparently Python does have os.waitpid(). That should consume less CPU time and provide a much faster response than, say, trying to kill the process at quarter-second intervals.


Addendum: As Niko points out, os.waitpid() may not work if the process is not a child of the current process. In that case, using os.kill(pid, 0) may indeed be the best solution. Note that, in general, there are three likely outcomes of calling os.kill() on a process:

  1. If the process exists and belongs to you, the call succeeds.
  2. If the process exists but belong to another user, it throws an OSError with the errno attribute set to errno.EPERM.
  3. If the process does not exist, it throws an OSError with the errno attribute set to errno.ESRCH.

Thus, to reliably check whether a process exists, you should do something like

def is_running(pid):        
    try:
        os.kill(pid, 0)
    except OSError as err:
        if err.errno == errno.ESRCH:
            return False
    return True
like image 52
Ilmari Karonen Avatar answered Oct 06 '22 17:10

Ilmari Karonen


Since that method would only work on linux, for linux/osx support, you could do:

import time
import os

def is_running(pid):
    stat = os.system("ps -p %s &> /dev/null" % pid)
    return stat == 0

pid = 64463

while is_running(pid):
    time.sleep(.25)

Edit - Per tMc's comment about excessive processes

Referencing: How to check if there exists a process with a given pid in Python?

Wouldn't this use less resources (I havent tested), than listing on the filesystem and opening FDs to all the results?

import time
import os

def is_running(pid):        
    try:
        os.kill(pid, 0)
    except OSError:
        return False

    return True

pid = 64463

while is_running(pid):
    time.sleep(.25)
like image 40
jdi Avatar answered Oct 06 '22 16:10

jdi


import time, then use time.sleep(#):

import time
process = get_process()
if process == None:
    #do something
else:
    #Wait until the process end
    while is_running(process):
        time.sleep(0.25)

I also have that exact same function in several of my scrips to read through /proc/#/cmdline to check for a PID.

like image 33
chown Avatar answered Oct 06 '22 16:10

chown