Linux デーモンとして Zope を動かす

まず rc スクリプト。最初の部分は chkconfig 用。優先度は 1 ~ 99

#!python
# chkconfig: 345 99 1
# description: Zope 3 service
# processname: runzope

import sys
import subprocess
from os import path


class Service(object):
	"""Linux service binding"""

	def __call__(self):
		for command in sys.argv[1:]:
			command = getattr(self, command, None)
			if callable(command):
				return command()

	def start(self):
		"""start service"""

	def stop(self):
		"""stop service"""

	def restart(self):
		"""restart service"""

	def pause(self):
		"""pause service"""

	def resume(self):
		"""resume service"""

	def status(self):
		"""show status of service"""


class Zope3Service(Service):
	"""Zope 3 service"""

	home = ''

	def __init__(self):
		super(Zope3Service, self).__init__()

	def _getpid(self):
		try:
			return int(open(path.join(self.home, 'var', 'Data.fs.lock'), 'r').read())
		except (IOError, ValueError, TypeError, ):
			return 0

	def _check(self):
		cmd = 'ps -p %s' % (self._getpid(), )
		pipe = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE).stdout
		for line in pipe.read().splitlines()[1:]:
			return True
		return False

	def _start(self):
		cmd = 'nohup %s &' % (path.join(self.home, 'bin', 'runzope'), )
		subprocess.Popen(cmd, shell = True)

	def start(self):
		if self._check():
			print 'already running'
			return
		self._start()

	def stop(self):
		cmd = 'kill %s' % (self._getpid(), )
		pipe = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE).stdout

	def restart(self):
		if self._check():
			self.stop()
		self._start()

	def status(self):
		if self._check():
			print 'Zope is running (%s).' % (self.home, )
		else:
			print 'Zope is not running (%s).' % (self.home, )


if __name__ == '__main__':
	zope = Zope3Service()
	zope.home = 'Zope インスタンスのパス'
	sys.exit(zope())

次に登録

chmod a+x スクリプト
ln スクリプト /etc/rc.d/サービス名
chkconfig --add サービス名