forked from ymc023/pushell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushelld.py
executable file
·72 lines (60 loc) · 1.99 KB
/
pushelld.py
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
#!/usr/bin/env python
# coding=utf8
#Author:ymc023
#Mail:[email protected]
#Platform:Centos7
#Date:Tue 01 Jan 2017 03:10:40 PM CST
'''
Usage:
./pushelld.py [status] [stop] [start] [restart]
Options:
status "查看服务状态"
stop "停止服务"
start "启动服务"
restart "重新启动服务"
Examples:
./pushelld.py status
'''
import sys
import time
import os
import shlex
from docopt import docopt
PUSHELLDIR=os.path.abspath(os.curdir)
RUN_SERVER='%s/run_server.py'%PUSHELLDIR
def bash(cmd):
return shlex.os.system(cmd)
def color_print(msg,color='red'):
color_dict = {'yellow':'\033[1;33m%s\033[0m',
'green':'\033[1;32m%s\033[0m',
'red':'\033[1;31m%s\033[0m'
}
msg = color_dict.get(color,'red') %msg
return msg
def pushelld():
arg = docopt(__doc__)
if arg['status']:
if str(bash("ps aux | grep 'run_server' | grep -v 'grep'")) in '0':
print (color_print('pushell is running ......','green'))
else:
print (color_print('pushell is not running ......','red'))
if arg['stop']:
if str(bash("ps aux | grep -E 'run_server.py' | grep -v grep | awk '{print $2}' | xargs kill\
-9 &>/dev/null")) in '0':
print(color_print('pushell is stop.','red'))
else:
print(color_print('pushell is not stop.','yellow'))
if arg['start']:
print(RUN_SERVER)
if str(bash("python %s &" %RUN_SERVER)) in '0':
print(color_print('pushell start is ok.','green'))
else:
print(color_print('pushell must be running as root.','red'))
if arg['restart']:
if str(bash("ps aux | grep -E 'run_server.py' | grep -v grep | awk '{print $2}' | xargs kill\
-9 &> /dev/null")) in '0' and str(bash("python %s &" %RUN_SERVER)) in '0':
print(color_print('pushell restart ok.','green'))
else:
print(color_print('pushell restart error.','red'))
if __name__ == '__main__':
pushelld()