-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathroutes.py
111 lines (88 loc) · 3.02 KB
/
routes.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
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
from flask import url_for, request
from util import url_decode, json_response, has_no_empty_params, tail
import requests
import urllib
import subprocess, time, sys
import configparser
import pandas as pd
import json
# '''
def run(cmd):
child = subprocess.Popen(
cmd, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr, shell=True)
return child
def routes(app):
"""app routes"""
@app.route('/')
def index():
"""index page"""
print('index')
r = {}
return json_response(200, r, True)
# 查询脚本运行进程 script:脚本名
@app.route("/process")
def query_process():
'''
/process?script=xxx
'''
print('query_process')
script_name = request.args.get("script") or ""
CMD = ["pgrep", "-f", script_name + '.py']
print(CMD)
child = subprocess.Popen(CMD, stdout=subprocess.PIPE, shell=False)
pid = child.communicate()[0].decode('utf-8')
if pid:
msg = '脚本%s正在运行, pid %s' % (script_name, pid)
r = {'msg': msg}
return json_response(200, r, True)
else:
msg = '脚本%s未运行!' % script_name
r = {'msg': msg}
return json_response(200, r, True)
# 结束脚本运行进程 script:脚本名
@app.route("/stop")
def query_stop():
'''
/stop?script=xxx
'''
print('query_stop')
script_name = request.args.get("script") or ""
child = subprocess.Popen(["pgrep", "-f", script_name + '.py'],
stdout=subprocess.PIPE,
shell=False)
pid = child.communicate()[0].decode('utf-8')
if pid:
CMD = 'kill -9 ' + pid
sub_child = run(CMD)
r = {'msg': pformat(sub_child)}
return json_response(200, r, True)
else:
msg = '脚本%s未运行!' % script_name
r = {'msg': msg}
return json_response(200, r, True)
# 启动脚本运行进程 script:脚本名, log:日志名
@app.route("/start")
def query_start():
'''
/start?script=xxx&log=output
'''
print('query_start')
script_name = request.args.get("script") or ""
LOGPATH = request.args.get("log") or "output"
LOGPATH += '.txt'
child = subprocess.Popen(["pgrep", "-f", script_name + '.py'],
stdout=subprocess.PIPE,
shell=False)
pid = child.communicate()[0].decode('utf-8')
if pid:
msg = '脚本%s已运行!' % script_name
r = {'msg': msg}
return json_response(200, r, True)
else:
# LOGPATH = 'output.txt'
CMD = 'nohup python -u ' + script_name + '.py > ' + LOGPATH + ' 2>&1 &'
sub_child = run(CMD)
r = {'msg': pformat(sub_child)}
return json_response(200, r, True)
if __name__=='__main__':
pass