Wait Until a Certain Process (Knowing the "Pid") End

Wait until a certain process (knowing the pid ) end

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

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

Sending signal 0 to a pid will raise an OSError exception if the pid is not running, and do nothing otherwise.

import os

def check_pid(pid):
""" Check For the existence of a unix pid. """
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True

How to check if a process id (PID) exists

To check for the existence of a process, use

kill -0 $pid

But just as @unwind said, if you want it to terminate in any case, then just

kill $pid

Otherwise you will have a race condition, where the process might have disappeared after the first kill -0.

If you want to ignore the text output of kill and do something based on the exit code, you can

if ! kill $pid > /dev/null 2>&1; then
echo "Could not send SIGTERM to process $pid" >&2
fi

wait for process to end in windows batch file

:loop
tasklist | find " 1234 " >nul
if not errorlevel 1 (
timeout /t 10 >nul
goto :loop
)

Get the list, search in the content, if found (on not found errorlevel = 1) wait and loop

To get the data into the variable

for /f %%a in ('tasklist ^| find /c " 1234 "') do set "var=%%a"

Python Gtk2 & Vte wait for the process Pid is finish

I've found a solution:

def __init__(self):
a=0
self.fenetre = gtk.Window(gtk.WINDOW_TOPLEVEL)
[...]
self.v = vte.Terminal()
# self.v.connect ("child-exited", lambda term: gtk.main_quit()) # this is the line to change
self.v.connect ("child-exited", lambda term: self.copie(self, a)) # the redirection after the process is finish
self.v.show()

def download(self, a, donnees=None):
child_pid = self.v.fork_command(None, ['/bin/bash', "./pluzz.sh", adresse])

def copie(self, a, donnees=None):
print "FINISH"


Related Topics



Leave a reply



Submit