如何实现Python启动、停止脚本
发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,这篇文章给大家分享的是有关如何实现Python启动、停止脚本的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。#!/usr/bin/python# -*- coding: UT
千家信息网最后更新 2025年12月01日如何实现Python启动、停止脚本
这篇文章给大家分享的是有关如何实现Python启动、停止脚本的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
#!/usr/bin/python# -*- coding: UTF-8 -*-import platform,osimport timeimport subprocessimport sysimport ctypesdef get_sysinfo(): sys = platform.system() return os.getpid(),sys def get_path(): p=os.path.split(os.path.realpath(__file__)) # ('D:\\workspace\\python\\src\\mysql', 'dao.py') p=os.path.split(p[0]) if not p: os.mkdir(p) return p[0]def get_pid_path(): return get_path() +'/tmp/yqs.pid'def check_pid(pid = 0,osname=''): if pid is None or pid == 0: return False wincmd = 'tasklist /FI "PID eq %s" /FI "IMAGENAME eq python.exe "' % str(pid) lincmd = 'ps ax |grep %s |grep python' % str(pid) cmd,size = (wincmd,150) if osname=='Windows' else (lincmd,20) returnstr=subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True) data = returnstr.stdout.read() return len(data) > size def read_pid(): if os.path.exists(get_pid_path()): try: with open(get_pid_path(),'r') as f: strpid = f.readline().strip() return int(strpid) except Exception : return None return Nonedef rm_pid(): if os.path.exists(get_pid_path()): os.remove(get_pid_path()) def kill(pid): """kill function for Win32""" kernel32 = ctypes.windll.kernel32 handle = kernel32.OpenProcess(1, 0, pid) return (0 != kernel32.TerminateProcess(handle, 0)) def check_run(): pid,osname = get_sysinfo() if not os.path.exists(get_pid_path()): with open(get_pid_path(),'w') as f: f.write(str(pid)) return False ''' 开始检查 ''' rs = check_pid(read_pid(),osname) if not rs : with open(get_pid_path(),'w') as f: f.write(str(pid)) return rs class Control : def start(self): if check_run(): print 'pro has run' else : print 'pro start...' time.sleep(1000) def stop(self): filePid = read_pid() if filePid is not None and filePid > 0: print 'pro has kill %s' % filePid kill(filePid) rm_pid() else : print 'Process has closed' def check(self): filePid = read_pid() if not filePid or not check_run() : message = "Process has closed\n" sys.stderr.write(message) else : message = "The process has been run, the process id:%d\n" sys.stderr.write(message % filePid) def helpInfo(self): print "usage: start|stop|check|help" if __name__ == "__main__": contr=Control() if len(sys.argv) == 2: param = sys.argv[1] if 'start' == param: contr.start() elif 'stop' == param: contr.stop() elif 'check' == param: contr.check() elif 'help' == param: contr.helpInfo() else: print r"not yes cmd" sys.exit(2) else: print "usage: %s start|stop|check|help" % sys.argv[0]感谢各位的阅读!关于"如何实现Python启动、停止脚本"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
脚本
内容
更多
篇文章
不错
实用
文章
看吧
知识
UTF-8
参考
帮助
有关
检查
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库三级考试速过
苹果系统服务器连接不了
医生让做dni数据库
软件开发未及时付尾款
生产分析软件开发商
提高网络安全意识意义
航天科技 卫星互联网
与艾尔登法环服务器断开连接
山东程鼎网络技术
电信网络安全专业就业方向
查看服务器带宽windows
光耀互联网信息科技
手机wifi网络安全
平安科技与互联网
服务器分之8 2
深圳开创网络技术有限公司
鹤舞云天服务器
免费 数据库 空间
plc怎么创建数据库
https需要服务器吗
问卷星网络安全
IBM服务器日志管理
网络技术二级难吗
深圳戴尔服务器厂家报价
合并数据库不存在或不受支持
魔兽任务网络安全
网络安全广告怎么消除
恐龙岛扫描不出服务器
三级网络技术有笔试吗
数据库模型与技术