January 12 four weeks five times "Python Foundation Advanced"

Source: Internet
Author: User

3.5 RC Script (definition of class and structure of script)
3.6 rc Script (Start method)
3.7 RC script (stop and Status method)
3.8 RC script (starts in daemon mode)

3.5 RC Script (definition of class and structure of script)/3.6 RC script (Start method)/3.7 RC script (stop and Status method)
Import sysimport osfrom subprocess import Popen, Pipeclass Process (object): ' memcached rc script ' def __init__ (s Elf, name, program, args, workdir): self.name = name Self.program = Program Self.args = args s            Elf.workdir = Workdir def _init (self): "/var/tmp/memcached" if not os.path.exists (Self.workdir): Os.mkdir (Self.workdir) Os.chdir (SELF.WORKDIR) def _pidfile (self): "'/VAR/TMP/MEMCACHED/MEMCAC            Hed.pid "Return Os.path.join (Self.workdir,"%s.pid "% self.name) def _writhpid (self): if self.pid: With open (Self._pidfile (), ' W ') as Fd:fd.write (str (self.pid)) def start (self): PID = self        . _getpid () If Pid:print ("%s is running ..."% self.name) sys.exit () Self._init ()        cmd = self.program + ' + Self.args p = Popen (cmd, stdout = PIPE, Shell = True) Self.pid = P.pid        Self._writhpid ()Print ("%s start sucessful"% self.name) def _getpid (self): p = Popen ([' pidof ', self.name], stdout = PIPE)            PID = P.stdout.read (). Strip () return PID def stop (self): PID = Self._getpid () if PID:            Os.kill (int (PID), +) if Os.path.exists (Self._pidfile ()): Os.remove (Self._pidfile ())        Print ("%s is stopped"% self.name) def restart (self): Self.stop () Self.start () def status: PID = Self._getpid () If Pid:print ("%s is already running"% self.name) Else:print ( '%s is not running '% self.name) def help (self): print ("Usage: {start | Stop | Startus | Restart} "% __file__) def main (): name = ' memcached ' prog = '/usr/bin/memcached ' args = '-u nobody-p 11211-c 102        4-m ' WD = '/var/tmp/memcached ' PM = Process (name = name, program = prog, args = args, Workdir = wd) Try: cmd = sys.argv[1] except Indexerror as E: Print ("Option error") Sys.exit () if cmd = = ' Start ': Pm.start () elif cmd = = ' Stop ': PM . Stop () elif cmd = = ' Restart ': Pm.restart () elif cmd = = ' status ': Pm.status () Else:pm.help ( if __name__ = = ' __main__ ': Main ()
3.8 RC script (starts in daemon mode)
Import sysimport osfrom subprocess import Popen, Pipeclass Process (object): ' memcached rc script ' ' args = {' USER ' : ' memcached ', ' PORT ': 11211, ' maxconn ': 1024x768, ' CACHESIZE ': +, ' OPTIONS ': '} def __init__ (self, Name, program, W Orkdir): self.name = name Self.program = Program Self.workdir = Workdir def _init (self): ' '/var/tmp/memcached ' if not os.path.exists (Self.workdir): Os.mkdir (Self.workdir) Os.chdir (s  ELF.WORKDIR) def _pidfile (self): '/var/tmp/memcached/memcached.pid ' return Os.path.join (Self.workdir,                "%s.pid"% self.name) def _writhpid (self): if Self.pid:with open (Self._pidfile (), ' W ') as FD:            Fd.write (str (self.pid)) def _readconf (self, f): with open (f) as Fd:lines = Fd.readlines () Return Dict ([I.strip (). Replace (' "', '). Split (' = ') for i in lines]) def _parseargs (self): conf = self . _readconf ('/etc/sysconfIg/memcached ') If ' user ' in conf:self.args[' user '] = conf[' user ' "if ' PORT ' in conf: self.args[' Port ' = conf[' port '] if ' maxconn ' in conf:self.args[' maxconn '] = conf[' maxconn '] if ' CACHESIZE ' in conf:self.args[' CACHESIZE ') = conf[' CACHESIZE '] if ' OPTIONS ' in conf:self.ar                   gs[' options ' = conf[' options '] options = ['-U ', self.args[' USER '], '-P ', self.args[' PORT ',  '-C ', self.args[' maxconn '], '-M ', self.args[' CACHESIZE '] Os.system ("chown%s%s"%        (self.args[' USER '], Self.workdir)) Return Options def start: pid = Self._getpid () If Pid:print ("%s is running ..."% self. Name) Sys.exit () self._init () cmd = [Self.program] + Self._parseargs () + ['-d ', '-P ', SELF._PIDFI Le ()] print (cmd) p = Popen (cmd, stdout = PIPE) #self. PID = P.pid #self. _wriThpid () print ("%s start sucessful"% self.name) def _getpid (self): p = Popen ([' pidof ', self.name], stdout = PIPE) pid = P.stdout.read (). Strip () return PID def stop (self): PID = Self._getpid () if p Id:os.kill (int (PID), +) if Os.path.exists (Self._pidfile ()): Os.remove (self._pidfile ()) Print ("%s is stopped"% self.name) def restart (self): Self.stop () Self.start () def stat            US (self): PID = Self._getpid () If Pid:print ("%s is already running"% self.name) Else: Print ("%s is not running"% self.name) def help (self): print ("Usage: {start | Stop | Startus | Restart} "% __file__) def main (): name = ' memcached ' prog = '/usr/bin/memcached ' args = '-u nobody-p 11211-c 102 4-m ' WD = '/var/tmp/memcached ' PM = Process (name = name, program = prog, Workdir = wd) Try:cmd = sys. ARGV[1] except Indexerror as E:print ("Option error") Sys.exit () if cmd = = ' Start ': Pm.start () elif cmd = = ' Stop ': Pm.stop () elif cmd = = ' Restart ': Pm.restart () elif cmd = = ' status ': Pm.status () Else:pm.hel P () if __name__ = = ' __main__ ': Main ()

January 12 four weeks five times "Python Foundation Advanced"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.