On 6/21/25 1:51 AM, c186282 wrote:
Just for fun :
# - - - - - - - - - - -
import os
# ---------------------
# Find matching insanc(es) of a search for
# running process(es).
# pNam is a list - the first item is the
# main search term, what grep uses to
# narrow things down.
# Subsequent items are used to narrow down
# results if desired - other words that must
# be in the return results. Note that we
# capitalize everything for convenience.
# All terms must match. The function returns
# list of entire ps lines matching PLUS the
# pid ... [[line,pid],...] ... or a null
# because these are what most want/need.
# Consider that to be an improved true/false.
#
def matchProc(pNam):
tmp=os.popen("ps -Af | grep -i "+pNam[0].upper()) # get prelim matches
del pNam[0] # not needed anymore
nPrm=len(pNam) # count of remaining
tmp=tmp.read() # get all from pipe
tmp=tmp.split('\n') # turn into text list
lt=len(tmp) # get number of txt lines
if lt==0 : return null # nothing found ? done.
# ok, we found SOMETHING so ...
outlist=[] # init output list
for line in tmp : # itereate lines
line=line.upper() # capitalize
mch=0 # init match counter
for n2 in range(0,len(pNam)) : # for each search term ...
if pNam[n2].upper() in line : mch=mch+1 # count found matches
if mch==nPrm : # if all matched ...
line=line.replace(chr(9),' ') # tabs ?
line=line.replace(" ",' ') # ez clean up of excess spaces
line=line.replace(" ",' ')
line=line.replace(" ",' ')
line=line.replace(' ',',') # finally, cvt space 2 commas
pid=line.split(',') # break and turn into a list
if len(pid)>1 : pid=pid[1] # isolate process ID
outlist.append([line,pid]) # add to output
if len(outlist)!=0 : > return outlist # return all complete matches
else : return null
# ==========================
# -------------
# Main
# -------------
print("out=",matchProc(['Python3','MyProg.py3']))
# - - - - - - - - - - - - -
Made this today - I'm sure someone can make it a
bit smaller and less-comprehensible - for a WATCHDOG
daemon. Runs every minute or so - and, if the
specified proc isn't running it'll re-spawn it.
The ability to find several matching instances
is a convenience to make the def more widely
usable.
There are a number of ways to find a process in
Python ... this is kind of an intermediate fix
employing both evoking command-line conveniences
up front and then kind of a multi-grep approach
further in. You may have multiple instances of
the same daemon too ... this will find them and
it'd be up to you to deal thereafter.
Note this ONLY uses the 'os' lib.
There IS a potential flaw here ... 'outstr' should
be set as null if there's no initial matches AND
if there's no cases of matches-all-params later.
Fixed it here. Then the later test is "if outlist!=null"
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)