一、一般模式
启动选项: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
97
98
99
100
101
102
103
104
105
106
107
108
109
|
#!/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、程序名、程序启动文件、执行参数、pid文件路径
self
.name
=
name
self
.program
=
program
self
.argv
=
argv
self
.workdir
=
workdir
def
__workDir(
self
):
# 判断pid文件路径,没有则创建
'''/var/tmp/memcached'''
if
not
os.path.exists(
"/var/tmp/memcached"
):
os.mkdir(
"/var/tmp/memcached"
)
os.chdir(
"/var/tmp/memcached"
)
def
__pidFile(
self
):
# 获取pid文件,带路径
'''/var/tmp/memcached/memcached.pid'''
return
os.path.join(
self
.workdir,
"%s.pid"
%
self
.name)
def
__writePid(
self
):
# 将pid写进pid文件中,pid为字符串类型
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
.__workDir()
cmd
=
self
.program
+
" "
+
self
.argv
p
=
Popen(cmd,stdout
=
PIPE,shell
=
True
)
# 启动进程方式
self
.pid
=
p.pid
# 加self,声明为类的属性,便于其他内部函数调用
self
.__writePid()
print
(
"%s start successful"
%
self
.name)
def
__getPid(
self
):
p
=
Popen([
"pidof"
,
self
.name],stdout
=
PIPE)
# 获取pid方式
pid
=
p.stdout.read().strip()
# 过滤pid
return
pid
def
stop(
self
):
pid
=
self
.__getPid()
if
pid:
os.kill(
int
(pid),
15
)
# 杀掉进程方式,pid为数值类型
if
os.path.exists(
self
.__pidFile()):
os.remove(
self
.__pidFile())
# 删掉pid文件
print
(
"%s is stopped"
%
self
.name)
def
restart(
self
):
self
.stop()
self
.start()
def
status(
self
):
pid
=
self
.__getPid()
if
pid:
print
(
"%s is running"
%
self
.name)
else
:
print
(
"%s is not running"
%
self
.name)
def
help
(
self
):
print
(
"Usage: %s {start|stop|restart|status}"
%
__file__)
def
main():
name
=
"memcached"
program
=
"/data/program/memcached/bin/memcached"
workdir
=
"/var/tmp/memcached"
argv
=
"-u nobody -p 11211 -c 1024 -m 64"
pm
=
Process(name
=
name,
program
=
program,
workdir
=
workdir,
argv
=
argv)
# 传参,也可以按顺序只写实参
try
:
cmd
=
sys.argv[
1
]
# 如启动参数有误,则抛出异常,并退出
except
Exception as e:
raise
e
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()
|
二、后台模式
说明:后台方式:-d -P XXX.pid ;如有配置文件,则配置文件参数替换掉默认参数
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
#!/usr/bin/env python
#encoding:utf-8
import
sys
import
os
from
subprocess
import
Popen, PIPE
class
Process(
object
):
'''memcached rc script'''
argv
=
{
"USER"
:
"memcached"
,
"PORT"
:
"11211"
,
"MAXCONN"
:
"1024"
,
"CACHESIZE"
:
"64"
,
"OPTIONS"
: ""}
# 默认启动参数
def
__init__(
self
,name,program,workdir):
# 初始化类,包含:self、程序名、程序启动文件、pid文件路径
self
.name
=
name
self
.program
=
program
self
.workdir
=
workdir
def
__readConf(
self
,f):
# 读取配置文件,返回字典
with
open
(f,
"r"
) as fd:
lines
=
fd.readlines()
return
dict
([i.strip().split(
"="
)
for
i
in
lines])
def
__parseArgv(
self
):
# 配置文件替换默认参数
'''/etc/sysconfig/memcached'''
conf
=
self
.__readConf(
"/etc/sysconfig/memcached"
)
if
"USER"
in
conf:
self
.argv[
"USER"
]
=
conf[
"USER"
]
if
"PORT"
in
conf:
self
.argv[
"PORT"
]
=
conf[
"PORT"
]
if
"MAXCONN"
in
conf:
self
.argv[
"MAXCONN"
]
=
conf[
"MAXCONN"
]
if
"CACHESIZE"
in
conf:
self
.argv[
"CACHESIZE"
]
=
conf[
"CACHESIZE"
]
options
=
[
"-u"
,
self
.argv[
"USER"
],
"-p"
,
self
.argv[
"PORT"
],
"-m"
,
self
.argv[
"CACHESIZE"
],
"-c"
,
self
.argv[
"MAXCONN"
]]
# 替换的参数,以列表形式返回
os.system(
"chown %s %s"
%
(
self
.argv[
"USER"
],
self
.workdir))
# 修改pid文件目录的属主为启动用户,才能写pid文件
print
(
"启动参数为:%s"
%
str
(options))
return
options
def
__workDir(
self
):
# 判断pid文件路径,没有则创建
'''/var/tmp/memcached'''
if
not
os.path.exists(
"/var/tmp/memcached"
):
os.mkdir(
"/var/tmp/memcached"
)
os.chdir(
"/var/tmp/memcached"
)
def
__pidFile(
self
):
# 获取pid文件,带路径
'''/var/tmp/memcached/memcached.pid'''
return
os.path.join(
self
.workdir,
"%s.pid"
%
self
.name)
def
start(
self
):
pid
=
self
.__getPid()
if
pid:
print
(
"%s is running"
%
self
.name)
sys.exit()
self
.__workDir()
cmd
=
[
self
.program]
+
self
.__parseArgv()
+
[
"-d"
,
"-P"
,
self
.__pidFile()]
# 启动参数,后台启动方式,按指定文件自动写pid
p
=
Popen(cmd,stdout
=
PIPE)
# 启动进程方式
print
(
"%s start successful"
%
self
.name)
def
__getPid(
self
):
p
=
Popen([
"pidof"
,
self
.name],stdout
=
PIPE)
# 获取pid方式
pid
=
p.stdout.read().strip()
# 过滤pid
return
pid
def
stop(
self
):
pid
=
self
.__getPid()
if
pid:
os.kill(
int
(pid),
15
)
# 杀掉进程方式,pid为数值类型
if
os.path.exists(
self
.__pidFile()):
os.remove(
self
.__pidFile())
# 删掉pid文件
print
(
"%s is stopped"
%
self
.name)
def
restart(
self
):
self
.stop()
self
.start()
def
status(
self
):
pid
=
self
.__getPid()
if
pid:
print
(
"%s is running"
%
self
.name)
else
:
print
(
"%s is not running"
%
self
.name)
def
help
(
self
):
print
(
"Usage: %s {start|stop|restart|status}"
%
__file__)
def
main():
name
=
"memcached"
program
=
"/data/program/memcached/bin/memcached"
workdir
=
"/var/tmp/memcached"
pm
=
Process(name
=
name,
program
=
program,
workdir
=
workdir)
# 传参,也可以按顺序只写实参
try
:
cmd
=
sys.argv[
1
]
# 如启动参数有误,则抛出异常,并退出
except
Exception as e:
raise
e
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()
|
本文转自 huangzp168 51CTO博客,原文链接:http://blog.51cto.com/huangzp/2048280,如需转载请自行联系原作者