Memcached 启动脚本

#!/usr/bin/env python

import sys
import os
from subprocess import Popen, PIPE

class 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
        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:
                fd.write(str(self.pid))

    def _getPid(self):
        p = Popen([‘pidof‘, self.name], stdout=PIPE)
        pid = p.stdout.read().strip()
        return 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‘]
        options = [‘-u‘, self.args[‘USER‘],
                   ‘-p‘, self.args[‘PORT‘],
                   ‘-m‘, self.args[‘CACHESIZE‘],
                   ‘-c‘, self.args[‘MAXCONN‘]]
        os.system("chown %s %s" % (self.args[‘USER‘], self.workdir))
        return options

    def start(self):
        pid = self._getPid()
        if pid:
            print "%s is already running...." % 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 stop(self):
        pid = self._getPid()
        if pid:
            os.kill(int(pid), 15)
            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: python %s {start|stop|status|restart}" % __file__

def main():
    name = ‘memcached‘
    prog = ‘/usr/bin/memcached‘
    wd = ‘/var/tmp/memcached‘
    pm = Process(name = name,
                 program = prog,
                 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()
时间: 2024-10-12 16:41:54

Memcached 启动脚本的相关文章

Python编写的memcached启动脚本

Python编写memcached启动脚本 一.rc 脚本的start.stop.restart.status方法 #/usr/bin/env python # -*- coding:utf-8 -*- # @time   :2018/1/12 19:11 # @Author :FengXiaoqing # @file   :rc.py import os import sys from subprocess import Popen,PIPE class Process(object):   

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

1.类的属性总结类属性,也是公有属性, 类的私有属性, 对象的共有属性, 对象的私有属性, 内置属性, 函数的局部变量, 全局变量, #/usr/bin/env python # -*- coding:utf-8 -*- class MyClass(object): var1 = '类属性,类的公有属性 var1' __var2 = '类的私有属性 __var2' def func1(self): self.var3 = '对象的公有属性 var3' self.__var4 = '对象的私有属性

Python - 利用python编写的memcached启动脚本

memcached作为缓存文件服务,默认是操作系统里面是可以直接yum -y install memcached进行安装的. /etc/init.d/memcached 是属于系统shell编写的管理脚本,下面这个脚本是python脚本编写出来的memcached管理脚本,和shell编写的脚本实现的效果一样. #!/usr/bin/python import sys import os from subprocess import Popen,PIPE class Process(object

Memcached启动脚本

假设memcached安装在/home/www/memcached目录 # cd /home/www/memcached # vi start.sh start.sh内容如下: bin/memcached -d -m 10240 -p 11211 -l 192.168.20.102  -c 1024 -u root -P /home/www/memcached/memcached.pid #chmod 755 start.sh 用脚本启动nginx # cd /home/www/memcache

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

ubuntu下memcached启动停止脚本

工作需求,需要自己写一个memcached的启动停止脚本,呕心沥血,终于完成,虽然很搓很搓,还是记录一下吧,废话不多说直接上脚本 #! /bin/sh # ckconfig: - 55 45 # description: The memcached daemon is a network memory cache service. # processname: memcached # config: /etc/memcached.conf # Source function library -

ELK相关启动脚本

elasticsearch启动脚本 #!/bin/bash # Description:Elasticsearch ORS SERVER ES_HOME=/usr/local/elasticsearch-5.0.0 PID=$(jps | grep Elasticsearch | awk '{print $1}') ES_STOP=$ES_HOME/bin/shutdown.sh ES_START=$ES_HOME/bin/startup.sh #Necessary environment va

启动脚本_《UNIX/LINUX 系统管理技术手册 第四版》

1.init 是系统引导起来之后第一个运行的进程,是系统最重要的守护进程,进程号始终为1. 2.随着机器的引导,init从运行级0开始,一级一级往上运行到/etc/inittab中所设置的默认运行级.当机器关闭时,将以相反顺序执行同样的处理过程. 3.启动脚本的主要文件位于/etc/init.d这个目录下面,每个脚本负责一个守护进程或者系统的某个特定方面. 4.为了实现不同运行级别下需要运行那些脚本(并带什么参数,K开头停止.S开头启动),当系统进入到一个新的运行级别时,不是直接在init.d目

real server 的一个启动脚本例子

real server 的vip 启动脚本 #!/bin/bash #chkconfig: 2345 50 50 #description: real server init script VIP=192.168.80.80 case $1 in "start") echo "starting real server ..." ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up echo &quo