3.6 rc脚本(start方法) 3.7 rc脚本(stop和status方法) 3.8 rc脚本(以daemon方式启动)

3.6-3.7 rc脚本(start、stop和status方法)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# [@Version](https://my.oschina.net/u/931210) : python 2.7
# [@Time](https://my.oschina.net/u/126678)    : 2018/12/10 0:54
# [@Author](https://my.oschina.net/arthor)  : zhang xun
# [@File](https://my.oschina.net/u/726396)    : rc.py
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(self.workdir)
			os.makedir(self.workdir)
			os.chdir(self.workdir)
		else :
			os.chdir(self.workdir)


	def _pidFile(self):
		'''/var/tmp/memcached/memcached.pid'''
		return os.path.join(self.workdir, "%s.pid" % self.name)

	def _withPid(self):
		if self.pid:
			with open(self._pidFile(),'w') as fd :
				fd.write(str(self.pid))
	def start(self):
		cmd = self.program + ' ' + self.args
		p = Popen(cmd,stdout=PIPE,shell=True)
		self.pid = p.pid
		self._withPid()
		print(" STARTED ! ")


	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 ("STOPED !")
	def restart(self):
		self.stop()
		self.stop()

	def status(self):
		pid = self._getPid()
		if pid :
			print (" started ")
		else :
			print (" stoped ")

3.8 rc脚本(以daemon方式启动)

猜你喜欢

转载自my.oschina.net/u/4030294/blog/2985991