[Tutor] Process problem
Hugo González Monteverde
hugonz-lists at h-lab.net
Wed Jun 15 20:30:30 CEST 2005
Hi Alberto,
Mmm I'm not sure if I get this all right. Why do you want to keep
cotascamon running after levantamuertos dies?? If you're using system(),
levantamuertos is waiting for cotascamon to finish completion before it
continues running, there should be no point where levantamuertos *runs*
while cotascamon also does (levantamuertos is sleeping)
One advice though: it is easier and more elegant if you, instead of
parsing the output of "ps" for a single process, you send it signal 0.
This will do nothing to the process itself, but will complain with a
traceback (OSError) if the pid does not exist. Like this:
from os import kill
from signals import SIG_DFL
pid = 16
try:
kill(pid, SIG_DFL)
except OSError:
print "Process does not exist..."
Please note this is untested as I'm os a Windows machine right now.... =/
Hope it helps,
Hugo
Alberto Troiano wrote:
> Hey all
>
> I have a problem with a program
>
> You see, I have a python script made in Python 2.2 that runs every 15
> minutes on Linux Red Hat 9.0
> This program (which code is attached below) named levantamuertos.py (spanish
> for death weakener) has to check in a database if the pids stored are
> running (note that it will be more than one pid) and if they are not it has
> to start again the process and it will store the new pid on the database
>
> The problem is that when I call it it runs and weaks all dead process but
> when it dies, he takes all his sons with him.
>
> How can I make that the cotascamon.py keep running although
> levantamuertos.py die?
>
> Thanks in advanced and here is the code
>
> Alberto
>
> ##################
> ### levantamuertos.py ###
> ##################
> import os
> import sys
> import MySQLdb
> import time
>
> class conexion(object):
> def __init__(self):
> self.db = MySQLdb.connect(host="localhost", user="administrador",
> passwd="123456",db="seguridad")
>
> def consulta(self,query,args=None):
> try:
> self.cursor=self.db.cursor()
> self.sql=self.cursor.execute(query,args)
> except:
> self.cerrar()
> else:
> self.cerrar()
>
> def cerrar(self):
> self.db.close()
>
> query="SELECT PID,user,cam,ruta from proceso where user='DMALAKIAN'"
> cur=conexion()
> cur.consulta(query)
> res=cur.cursor.fetchall()
> if len(res)>0:
> for elem in res:
> pi=elem[0]
> ps_command = 'ps -p %s' % pi
> pidchk = os.popen(ps_command).readlines()
> count = 0
> for line in pidchk:
> count +=1
> if count > 1:
> print "Encontre el proceso"
> else:
> query="update proceso set PID=%s where user='DMALAKIAN'"
> p=os.getpid()
> args=(p)
> cur=conexion()
> cur.consulta(query,args)
> query="SELECT PID,user,cam,ruta from proceso where user <>
> 'DMALAKIAN'"
> cur=conexion()
> cur.consulta(query)
> res=cur.cursor.fetchall()
> if len(res)>0:
> for elem in res:
> pid=elem[0]
> user=elem[1]
> cam=elem[2]
> ruta=str(elem[3])
> ps_command = 'ps -p %s' % pid
> pidchk = os.popen(ps_command).readlines()
> count = 0
> for line in pidchk:
> count +=1
> if count > 1:
> pass
> else:
> os.system("python2.2 /root/cotascamon.py
> "+str(ruta[19:len(ruta)])+" "+user+" "+str(cam))
> else:
> pass
> else:
> query="insert into proceso values('DMALAKIAN',0,%s,'NO HAY RUTA PARA
> ESTE USUARIO')"
> p=os.getpid()
> args=(p)
> cur=conexion()
> cur.consulta(query,args)
> query="SELECT PID,user,cam,ruta from proceso where user <> 'DMALAKIAN'"
> cur=conexion()
> cur.consulta(query)
> res=cur.cursor.fetchall()
> if len(res)>0:
> for elem in res:
> pid=elem[0]
> user=elem[1]
> cam=elem[2]
> ruta=str(elem[3])
> ps_command = 'ps -p %s' % pid
> pidchk = os.popen(ps_command).readlines()
> count = 0
> for line in pidchk:
> count +=1
> if count > 1:
> pass
> else:
> os.system("python2.2 /root/cotascamon.py
> "+str(ruta[19:len(ruta)])+" "+user+" "+str(cam))
> else:
> pass
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list