• Python学习-复习7次课(12月4日)


    复习7次课(12月4日)
    3.3 类的属性总结
    3.4 类的方法总结
    3.5 rc脚本(类的定义与脚本的结构)
    3.6 rc脚本(start方法)
    3.7 rc脚本(stop和status方法)
    3.8 rc脚本(以daemon方式启动)

    类的属性总结
    类属性,也是共有属性
    类的私有属性
    对象的共有属性
    对象的私有属性
    内置属性
    函数的局部变量
    全局变量


    类的方法总结
    公有方法
    私有方法
    类方法
    静态方法
    内置方法


    rc脚本(类的定义与脚本的结构)
    rc脚本(start方法)
    rc脚本(stop和status方法)
    rc脚本(以daemon方式启动)

    #!/usr/bin/env python

    import sys
    import os
    from subprocess import Popen, PIPE

    class 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):
    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 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), 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: %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()

  • 相关阅读:
    重定向 重写
    php 安装 event 和 libevent 扩展
    curl 和 tcpdump
    yum 升级php版本
    shell 教程
    shell脚本 inotify + rsync 同步脚本
    nodesj中 中间件express-session的理解
    node.js中express-session配置项详解
    我对面向对象的理解
    BootstrapValidator
  • 原文地址:https://www.cnblogs.com/zhuntidaoren/p/7985115.html
Copyright © 2020-2023  润新知