-
Notifications
You must be signed in to change notification settings - Fork 0
/
staop.py
executable file
·52 lines (48 loc) · 1.59 KB
/
staop.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
#!/usr/bin/python
# coding=utf-8
import sys
import os
from config_util import *
def start(name, cmd):
sh = 'nohup %s > log/%s.out 2>&1 &'%(cmd, name)
ret = os.system(sh)
if ret != 0:
print 'Something wrong! cmd: %s'%sh
def stop(name):
grep = 'ps x'
for nm in name.split(':'):
grep += '|grep %s'%nm
sh = """%s|grep -v grep|grep -v staop|awk '{print $1}'"""%grep
pids = os.popen(sh).read().split('\n')
sh = """%s|grep -v grep|grep -v staop|awk '{for(i=5;i<=NF;i++)printf $i" ";printf "\\n"}'"""%grep
cmds = os.popen(sh).read().split('\n')
process = [(pids[i], cmds[i]) for i in range(len(pids)) if len(pids[i]) > 0]
if len(process) > 1:
print '\n'.join(cmds)
print 'Process matched more than 1, continue? y/n'
c = raw_input()
if c != 'y':
return
for pid, cmd in process:
sh = 'kill -9 %s'%pid
ret = os.system(sh)
if ret != 0:
print 'Something wrong! cmd: %s'%sh
if __name__ == '__main__':
usage = 'Usage: python staop.py start|stop appname\n\
@appname must from staop.cfg when start a process;\n\
@appname when stop a process, parts(splited by ":") used to grep process.'
try:
operation = sys.argv[1]
name = sys.argv[2]
if operation == 'start':
cf = ConfigUtil('staop.cfg')
cmd = '%s --mark=%s'%(cf.get(name, 'command'), name)
start(name, cmd)
elif operation == 'stop':
stop(name)
else:
print usage
except Exception, e:
print str(e)
print usage