python—memcached启动脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python—memcached启动脚本相关的知识,希望对你有一定的参考价值。

一、一般模式

    启动选项:start、stop、restart、status

#!/usr/bin/env python
#encoding:utf-8

import sys
import os
from subprocess import Popen, PIPE

class Process(object):
    '''memcached rc script'''
    def __init__(self,name,program,argv,workdir):  # 初始化类,包含:self、程序名、程序启动文件、执行参数、pid文件路径
        self.name = name
        self.program = program
        self.argv = argv
        self.workdir = workdir
     
        
    def __workDir(self):   # 判断pid文件路径,没有则创建
        '''/var/tmp/memcached'''
        if not os.path.exists("/var/tmp/memcached"):
            os.mkdir("/var/tmp/memcached")
            os.chdir("/var/tmp/memcached")
            
    def __pidFile(self):   # 获取pid文件,带路径
        '''/var/tmp/memcached/memcached.pid'''
        return os.path.join(self.workdir,"%s.pid" % self.name)
        
    def __writePid(self):  # 将pid写进pid文件中,pid为字符串类型
        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.__workDir()
        cmd = self.program + " " + self.argv
        p = Popen(cmd,stdout=PIPE,shell=True)   # 启动进程方式
        self.pid = p.pid     # 加self,声明为类的属性,便于其他内部函数调用
        self.__writePid()
        print("%s start successful" % self.name)
     
        
    def __getPid(self):
        p = Popen(["pidof",self.name],stdout=PIPE)  # 获取pid方式
        pid = p.stdout.read().strip()    # 过滤pid                 
        return pid
        
    def stop(self):
        pid = self.__getPid()
        if pid:
            os.kill(int(pid),15)         # 杀掉进程方式,pid为数值类型
            if os.path.exists(self.__pidFile()):
                os.remove(self.__pidFile())     # 删掉pid文件
            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)    # 传参,也可以按顺序只写实参
    try:
        cmd = sys.argv[1]  # 如启动参数有误,则抛出异常,并退出
   
    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 == "status":
        pm.status()
        
    else:
        pm.help()
        
if __name__ == "__main__":
    main()



二、后台模式

   说明:后台方式:-d -P XXX.pid ;如有配置文件,则配置文件参数替换掉默认参数

#!/usr/bin/env python
#encoding:utf-8

import sys
import os
from subprocess import Popen, PIPE

class Process(object):
    '''memcached rc script'''
     
    argv = {"USER": "memcached",
            "PORT": "11211",
            "MAXCONN": "1024",
            "CACHESIZE": "64",
            "OPTIONS": ""}   # 默认启动参数
            
    def __init__(self,name,program,workdir):  # 初始化类,包含:self、程序名、程序启动文件、pid文件路径
        self.name = name
        self.program = program
        self.workdir = workdir
    
        
    def __readConf(self,f):    # 读取配置文件,返回字典
        with open(f,"r") as fd:
            lines = fd.readlines()
            return dict([i.strip().split("=") for i in lines])
            
    def __parseArgv(self):    # 配置文件替换默认参数
    '''/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"] = conf["CACHESIZE"]
        
        options = ["-u",self.argv["USER"],
                   "-p",self.argv["PORT"],
                   "-m",self.argv["CACHESIZE"],
                   "-c",self.argv["MAXCONN"]]  # 替换的参数,以列表形式返回
                   
        os.system("chown %s %s" % (self.argv["USER"],self.workdir)) # 修改pid文件目录的属主为启动用户,才能写pid文件
        print ("启动参数为:%s" % str(options))
        return options
        
    def __workDir(self):   # 判断pid文件路径,没有则创建
        '''/var/tmp/memcached'''
        if not os.path.exists("/var/tmp/memcached"):
            os.mkdir("/var/tmp/memcached")
            os.chdir("/var/tmp/memcached")
            
    def __pidFile(self):   # 获取pid文件,带路径
        '''/var/tmp/memcached/memcached.pid'''
        return os.path.join(self.workdir,"%s.pid" % self.name)
        
    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()]  # 启动参数,后台启动方式,按指定文件自动写pid
        p = Popen(cmd,stdout=PIPE)   # 启动进程方式
        print("%s start successful" % self.name)
     
        
    def __getPid(self):
        p = Popen(["pidof",self.name],stdout=PIPE)  # 获取pid方式
        pid = p.stdout.read().strip()    # 过滤pid                 
        return pid
        
    def stop(self):
        pid = self.__getPid()
        if pid:
            os.kill(int(pid),15)         # 杀掉进程方式,pid为数值类型
            if os.path.exists(self.__pidFile()):
                os.remove(self.__pidFile())     # 删掉pid文件
            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)    # 传参,也可以按顺序只写实参
    try:
        cmd = sys.argv[1]  # 如启动参数有误,则抛出异常,并退出
   
    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 == "status":
        pm.status()
        
    else:
        pm.help()
        
if __name__ == "__main__":
    main()


以上是关于python—memcached启动脚本的主要内容,如果未能解决你的问题,请参考以下文章

使用 python 实现 memcached 的启动服务脚本 rc

Python中的类方法及属性总结举例,编写memcached启动脚本举例

分享Memcached shell启动停止脚本

用于重新启动 memcached 的 Cron 脚本不起作用

Memcached 安装脚本(附服务器自启动)

常用python日期日志获取内容循环的代码片段