2012年6月19日 星期二

簡明詞典

價值觀 - 想法與做法 - by Wong Sir
Process scheduling - What process should run on next time and how long
聰明- 做正確的事情, 醒目- 做好一件事 by Tam Sir

2012年6月13日 星期三

mon process via pid file



#!/usr/bin/python3.2
import os

self_pid_path = '/var/run/monPid.pid'
pid_path_list = ['/var/run/mldonkey.pid', '/var/run/sshd.pid']

# olny a instance run

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

pid_file=None

if os.path.exists(self_pid_path):
with open(self_pid_path, 'r') as oldpid_file:
pid = oldpid_file.readline()
if pid.isdigit() and check_pid(int(pid)):
exit()
else:
pid_file = open(self_pid_path, 'w')
else:
pid_file = open(self_pid_path, 'a')

pid_file.write(str(os.getpid()))
pid_file.close()

# olny a instance run

# detect mldonkey alive by pid file

for pid_path in pid_path_list:
pid = 0
program = pid_path[pid_path.rfind('/')+1:-4]
if os.path.exists(pid_path):
with open(pid_path, 'r') as pid_file:
pid = pid_file.readline()[:-1]
if pid.isdigit() and check_pid(int(pid)):
print (program + ': alive')
continue
else:
pid = ''

if pid.isdigit() and check_pid(int(pid)):
print (program + '(' + pid + ')' + 'alive')
else:
print (program + '(' + pid + ')' + 'dead')
os.system('echo `date +"%a %b %d %T"` /etc/init.d/' + program + ' restart >> /var/log/monPidFile')



crontab -e
* * * * * /sbin/monPidFile