我想做这样的事情:
try:
pid = int(file(lock_file, "r").read())
print "%s exists with pid: %s" % (lock_file, pid)
if not check_pid(pid):
print "%s not running. Phantom lock file? Continuing anyways" % pid
elif wall_time(pid) > 60 * 5:
print "%s has been running for more than 5 minutes. Killing it" % pid
os.kill(pid)
else:
print "Exiting"
sys.exit()
except IOError:
pass
lock = file(lock_file, "w")
lock.write("%s" % os.getpid())
lock.close()
如何实现 wall_time
?我必须从 /proc
读取还是有更好的方法?
最佳答案
或许您可以查看锁定文件的创建时间。这不能保证是正确的,但在大多数情况下都是正确的(而且弄错的后果很小)。
关于Python:进程运行的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1899922/