python rc脚本实例续

##
脚本功能:使用python写一个memcached的start/stop/restart/status脚本。
前面构造了脚本的基础结构,下面是具体方法的实现。

命令行启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/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 _writePid(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._writePid()
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()

使用命令行启动,父进程为当前bash的PID,当前shell关闭之后,该子进程由init接受,即父进程变更为1。
查询当前bash的PID

  • 方法1

    1
    2
    [root@linux02 advance]# echo $$
    98509
  • 方法2

    1
    2
    3
    4
    5
    6
    7
    [root@linux02 advance]# tty
    /dev/pts/0
    [root@linux02 advance]# ps -ef|grep pts/0
    root 98501 1666 0 Jun20 ? 00:00:10 sshd: root@pts/0
    root 98509 98501 0 Jun20 pts/0 00:00:01 -bash
    root 109237 98509 0 11:35 pts/0 00:00:00 ps -ef
    root 109238 98509 0 11:35 pts/0 00:00:00 grep pts/0

守护进程启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/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 _writePid(self):
if self.pid:
with open(self._pidFile(), 'w') as fd:
fd.write(str(self.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 running..." % self.name
sys.exit()
self._init()
cmd = [self.program] + self._parseArgs() + ['-d','-P', self._pidFile()]
print cmd
p = Popen(cmd, stdout=PIPE)
#self.pid = p.pid
#self._writePid()
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,
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()

注意点

  1. -d方式启动,需添加-P参数,不需要自己写入pid文件。
  2. 守护进程启动方式在命令行启动脚本基础上进行了改进,主要是针对配置文件。
  3. 守护进程方式启动,会出现二次fork,最后结果就是其父进程PID为1。

Recommended Posts