memcached startup script written by Python

Source: Internet
Author: User

Python writes memcached startup script


First, the RC script start, stop, restart, status method

#/usr/bin/env python# -*- coding:utf-8 -*-#  @time    :2018/1/12  19:11#  @Author  :FengXiaoqing#  @file    :rc.pyimport osimport sysfrom  subprocess import popen,pipeclass process (object):     "memcached  rc script '     def __init__ (self,name,program,args,workdir):         self.name = name         Self.program = program        self.args = args         self.workdir = workdir    def  _init (self):         "/var/tmp/memcached"          if not os.path.exists (Self.workdir):           &nBsp; os.mkdir (Self.workdir)              Os.chdir (Self.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 start (self):         pid = self._getpid ( )         if pid:             print "%s is already runnig ..."  % 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   &nbsP;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 (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:  %s {start|stop|status|restart} ")  % __file__def main ():     name  =  ' memcached '     prog =  '/usr/bin/memcached '      args =  '-u nobody -p 11211 -c 1024 -m 64 '      wd =  '/var/tmp/memcached '     pm = process (name = name,                  program=prog,                  args=args,                  WORKDIR=WD)      try:        cmd = sys.argv[1]     except indexerror,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 () 

Second, RC script (start in daemon)

#/usr/bin/env python# -*- coding:utf-8 -*-#  @time    :2018/1/12  21:18#  @Author  :FengXiaoqing#  @file    :rc-d.pyimport osimport sysfrom  subprocess import popen,pipeclass process (object):     "memcached  rc script '     args = {' USER ': ' memcached ',              ' PORT ':11211,              ' Maxconn ':1024,              ' CACHESIZE ':64,             ' OPTIONS ':  ' '}    def __init__ (self,name,program,workdir):         self.name = name        self.program =  program   &Nbsp;    self.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/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:         &nbSp;       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 '] =&NBsp;conf[' PORT ']        if  ' maxconn '  in conf:             self.args[' MAXCONN '] = conf[' Maxconn ']        if  ' CACHESIZE '  in conf:             self.args[' CACHESIZE '] = conf[' CACHESIZE ']        options = ['-u ', self.args[' USER '],                     '- P ', self.args[' PORT '],                     '-M ', self.args[' Maxconn '],                     '-C ', self.args[' CACHESIZE ']]          return options    def start (self):         pid  = self._getpid ()         if pid:             print  "%s is already runnig ..."  % self.name            sys.exit ()          self._init ()         cmd  = [self.program]+self._parseargs () +['-d ', '-P ', Self._pidfile ()]         p = popen (cmd,stdout=pipe)         print  "%s  start sucessful. "  % self.name    def _getpid (self):         p = popen ([' pidof ', self.name],stdout = pipe)        &Nbsp; 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 (self):     &nbsP;   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: %s {start|stop|status|restart}")  % __file__def main ():     name =  ' memcached '     prog =  '/usr/bin/memcached '     wd =  '/var/tmp/memcached '     pm = process (name  = name,                  program=prog,&nBsp;                workdir=wd )     try:        cmd = sys.argv[1]     except IndexError,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 () 



memcached startup script written by Python

Related Article

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.