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 = ''