one or one-like mode
Startup options: Start, stop, restart, status
#!/usr/bin/env python#encoding:utf-8import sysimport osfrom subprocess import Popen, pipeclass process (object): "Memcached rc script" def __init__ (SELF,NAME,PROGRAM,ARGV,WORKDIR): # initialization class, containing: Self, program name, program startup file , execution parameters, PID file path self.name = name self.program = program Self.argv = argv self.workdir = workdir def __workdir (self): # judgment pid file path, none created "'/var/tmp/memcached ' if not os.path.exists ("/var/ Tmp/memcached "): os.mkdir ("/var/tmp/memcached") os.chdir ("/var/tmp/memcached") def __pidfile (self): # get PID file with path '/var/tmp/memcached/memcached.pid ' return os.path.join (Self.workdir, "%s.pid" % Self.name) def __writepid (self): # PID is written into the PID file, the PID is the string type if self.pid: with open (Self.__pidFile (), "W") as fd: fd.write (str (SELF.PID)) def start (self): pid = self.__getpid () # judgment process, exiting if already exists if pid: print ("%s is running" % self.name) sys.exit () self.__workdir () cmd = self.program + " " + self.argv p = popen (cmd,stdout=pipe,shell=true) # START Process mode self.pid = p.pid # add self, declared as a property of a class, facilitates other internal function calls self.__writepid () print ("%s start successful" % self.name) def __getpid (self): p = popen (["Pidof", Self.name],stdout=pipe) # Get PID mode pid = p.stdout.read (). Strip () # Filter pid return pid def stop (self): pid = self.__getpid () if pid: &nbSp; os.kill (int (PID), # ) Kill process mode, PID is numeric type if os.path.exists ( Self.__pidfile ()): os.remove (Self.__pidfile ()) # remove PID file print ("%s is stopped" % self.name) def restart (self): self.stop () self.start () def status (self): pid = self.__getpid () if pid: print ("%s is running" % self.name) else: print ("%s is not running" % self.name) def help (self): print ("Usage: %s {start|stop|restart|status}" % __file__) def main (): name = "memcached" program = "/data/program/memcached/ Bin/memcached " workdir = "/var/tmp/memcached " argv = "-u nobody -p 11211 -c 1024 -m 64 " pm = process (name = name, program = program, workdir = workdir,           ARGV = ARGV) # Pass the parameter, can also be in order only realistic parameter try: cmd = sys.argv[1] # If the startup parameter is incorrect, throw an exception and exit except exception as e: raise e sys.exit () if cmd == "Start":  &NBsp; 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, backstage mode
Description: Background mode:-d-p xxx.pid; If you have a configuration file, the profile parameter replaces the default parameter
#!/usr/bin/env python#encoding:utf-8import sysimport osfrom subprocess import Popen, pipeclass process (object): "Memcached rc script" argv = {"USER": "memcached", "PORT": "11211", "Maxconn": "1024x768", "CACHESIZE": ", " "OPTIONS": ""} # default startup Parameters def __init__ (Self,name,program,workdir): # initialization class, including: Self, program name, program startup file, PID file path self.name = name self.program = program self.workdir = workdir def __readconf (self,f): # read configuration file, return dictionary with open (F, "R") as fd: lines = fd.readlines () return dict ([I.strip (). Split ("=") for i in lines]) def __parseargv (self): # configuration file Replace default parameter '/etc/sysconfig/memcached ' conf = self.__readconf ("/etc/sysconfig/memcached") if "USER" in conf: self.argv["User"] = conf["user"] if "PORT" in conf: self.argv["Port"] = conf["Port"] if "Maxconn" in conf: self.argv["Maxconn"] = conf["MAXCONN"] if "CACHESIZE" in conf: self.argv["CACHESIZE"]&Nbsp;= conf["CACHESIZE"] options = ["-U", self.argv["USER"], "-P", self.argv["PORT"], "-M", SELF.ARGV ["CACHESIZE"], "-C", self.argv["Maxconn"]] # replaced parameters, returned as a list os.system ("chown %s %s" % (self.argv["USER"],self.workdir)) # Modify PID file directory owner to start the user, in order to write PID file print ("Startup parameter:%s" %  STR (options)) return options def __workdir (self): # judgment pid file path, none created '/var/tmp/memcached ' if Not os.path.exists ("/var/tmp/memcached"): os.mkdir ("/var/tmp/memcached") os.chdir ("/var/tmp/memcached") def __pidfile (self): # get PID file with path '/var/tmp/memcached/memcached.pid ' Return os.path.join (Self.workdir, "%s.pid" % self.name) &nbSp; def start (self): pid = self.__getpid () if pid: print ("%s is running" % self.name) sys.exit () self.__workdir () cmd =  [SELF.PROGRAM] + SELF.__PARSEARGV () + ["-D", "-P", self.__pidfile ()] # startup parameters, background startup mode, auto-write Pid p = popen (cmd) by specified file Stdout=pipe) # boot process mode print ("%s Start successful " % self.name) def __getpid (self): p = popen ([" Pidof ", self.name],stdout=pipe) # get PID mode pid = p.stdout.read (). Strip () # filter pid return pid def stop ( Self): pid = self.__getpid () if pid: Os.kill (int (PID),) # kill process mode, PID is numeric type if os.path.exists (Self.__pidFile ()): &nBsp; os.remove (Self.__pidfile ()) # Remove the PID file print ("%s is stopped " % self.name) def restart (self): self.stop () self.start () def status (self): pid = self.__getpid () if pid: print ("%s is running" % self.name) else: print ("%s is not running" % Self.name) def help (self): print ("Usage : %s {start|stop|restart|status} " % __file__) def main (): name = "memcached" program = "/data/program/memcached/bin/memcached" workdir = "/var/tmp/memcached" pm = process (name = name, program = program, workdir = workdir) # pass-through, can also be in order only realistic reference try: cmd = sys.argv[1] # throws an exception if the startup parameter is incorrect, and exits except Exception as e: raise e sys.exit () if cmd == "Start": pm.start () elif cmd == "Stop": pm.stop () elif cmd == "Restart": pm.restart () elif cmd ==&nbSP; " Status ": pm.status () else: pm.help () if __name__ == "__main__": main ()
python-memcached Startup scripts