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:
- If the process exists and belongs to you, the call succeeds.
- If the process exists but belong to another user, it throws an
OSError
with theerrno
attribute set toerrno.EPERM
. - If the process does not exist, it throws an
OSError
with theerrno
attribute set toerrno.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
If-Condition With Multiple Actions in Robot Framework
How to Modify the Navigation Toolbar Easily in a Matplotlib Figure Window
Importerror: No Module Named Sklearn (Python)
Valueerror: X and Y Must Be the Same Size
Loading All Images Using Imread from a Given Folder
Using Buttons in Tkinter to Navigate to Different Pages of the Application
How to Check Url Change With Selenium in Python
Psycopg2 Insert Python Dictionary as Json
Python - How to Check If Table Exists
Split String in a Spark Dataframe Column by Regular Expressions Capturing Groups
How Can My Model Primary Key Start With a Specific Number
Pickle - Cpickle.Unpicklingerror: Invalid Load Key, '?'
_Tkinter.Tclerror: No Display Name and No $Display Environment Variable
How to Add Pandas Data to an Existing CSV File
Most Efficient Way to Construct Similarity Matrix
Is There a Short-Hand for Nth Root of X in Python
Typeerror: Unsupported Operand Type(S) for ** or Pow(): 'List' and 'Int'