In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to implement Python start and stop scripts. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
#! / 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 'start check''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] Thank you for reading! This is the end of the article on "how to start and stop Python script". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
Server environment * system: CentOS 6.5pcre-8.39
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.