1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #! /usr/bin/env python
# -*- coding: ISO8859-1 -*-
#
#
from subprocess import Popen,PIPE
from os import waitpid, popen, kill
from time import localtime, sleep
from sys import argv, exit
ind = 0
# utilisation python process.py xxHxx
try:
if argv[1][2:-2] == 'h' or argv[1][2:-2] == 'H':
tempfin = int(argv[1][:-3]) * 60 + int(argv[1][-2:])
except: exit(1)
starttime = int(localtime()[3]) * 60 + int(localtime()[4])
if tempfin < starttime:
print 'Erreur dans la durée, l\'heure de fin est déjà passée.'
exit(1)
cmd = 'ls -R' # par exemple
print 'Duree de depart ', starttime
print 'Duree max : ', tempfin
p = Popen(cmd, stdout=PIPE, shell=True)
sts = waitpid(p.pid, 1)
print "Lancement ok avec PID ",p.pid
fils = popen('ps -Al | grep ' + str(p.pid))
print 'Tous les PID :'
listPID = []
for lignes in fils:
longl = len(lignes)
print str(lignes [11:-(longl-15)])
listPID.append(str(lignes [11:-(longl-15)]))
while True:
if tempfin > int(localtime()[3]) * 60 + int(localtime()[4]):
ind += 1
print 'En cour : pass' , ind
sleep(1)
elif tempfin == int(localtime()[3]) * 60 + int(localtime()[4]):
print 'Fin par kill'
for items in listPID:
print 'Kill de ',items
kill(int(items), 9)
print 'Heure de fin :',str(localtime()[3]) + 'h' + str(localtime()[4])
break
else:
print 'Fin normale'
print 'Heure de fin :',str(localtime()[3]) + 'h' + str(localtime()[4])
break
exit(0) |
Partager