From 0af687f78113dc0cb55b70636ecb8c9fdcd8c7f2 Mon Sep 17 00:00:00 2001 From: whyour Date: Mon, 1 May 2023 22:56:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=81=9C=E6=AD=A2=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E5=91=BD=E4=BB=A4=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/system.ts | 19 ++++++++++++++++++- back/config/util.ts | 7 +++---- back/services/system.ts | 20 +++++++++++++++++++- shell/bot.sh | 2 +- shell/check.sh | 2 +- 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/back/api/system.ts b/back/api/system.ts index 457ff413..7d1f0711 100644 --- a/back/api/system.ts +++ b/back/api/system.ts @@ -161,7 +161,6 @@ export default (app: Router) => { }), }), async (req: Request, res: Response, next: NextFunction) => { - const logger: Logger = Container.get('logger'); try { const systemService = Container.get(SystemService); const uniqPath = await getUniqPath(req.body.command); @@ -188,4 +187,22 @@ export default (app: Router) => { } }, ); + + route.put( + '/command-stop', + celebrate({ + body: Joi.object({ + command: Joi.string().required(), + }), + }), + async (req: Request, res: Response, next: NextFunction) => { + try { + const systemService = Container.get(SystemService); + const result = await systemService.stop(req.body); + res.send(result); + } catch (e) { + return next(e); + } + }, + ); }; diff --git a/back/config/util.ts b/back/config/util.ts index 48f41ada..f183eec3 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -496,10 +496,9 @@ export async function killTask(pid: number) { } export async function getPid(name: string) { - let taskCommand = `ps -ef | grep "${name}" | grep -v grep | awk '{print $1}'`; - const execAsync = promisify(exec); - let pid = (await execAsync(taskCommand)).stdout; - return Number(pid); + const taskCommand = `ps -eo pid,command | grep "${name}" | grep -v grep | awk '{print $1}' | head -1 | xargs echo -n`; + const pid = await promiseExec(taskCommand); + return pid ? Number(pid) : undefined; } interface IVersion { diff --git a/back/services/system.ts b/back/services/system.ts index d7895f3c..30e5c69a 100644 --- a/back/services/system.ts +++ b/back/services/system.ts @@ -9,7 +9,12 @@ import ScheduleService, { TaskCallbacks } from './schedule'; import { spawn } from 'child_process'; import SockService from './sock'; import got from 'got'; -import { parseContentVersion, parseVersion } from '../config/util'; +import { + getPid, + killTask, + parseContentVersion, + parseVersion, +} from '../config/util'; import { TASK_COMMAND } from '../config/const'; @Service() @@ -178,4 +183,17 @@ export default class SystemService { } this.scheduleService.runTask(`real_time=true ${command}`, callback); } + + public async stop({ command }: { command: string }) { + if (!command.startsWith(TASK_COMMAND)) { + command = `${TASK_COMMAND} ${command}`; + } + const pid = await getPid(command); + if (pid) { + await killTask(pid); + return { code: 200 }; + } else { + return { code: 400, message: '任务未找到' }; + } + } } diff --git a/shell/bot.sh b/shell/bot.sh index f5dc8344..d3a7ef3f 100755 --- a/shell/bot.sh +++ b/shell/bot.sh @@ -42,6 +42,6 @@ echo -e "\npython3依赖安装成功...\n" echo -e "4、启动bot程序...\n" make_dir $dir_log/bot cd $dir_data -ps -ef | grep "python3 -m jbot" | grep -v grep | awk '{print $1}' | xargs kill -9 2>/dev/null +ps -eo pid,command | grep "python3 -m jbot" | grep -v grep | awk '{print $1}' | xargs kill -9 2>/dev/null nohup python3 -m jbot >$dir_log/bot/nohup.log 2>&1 & echo -e "bot启动成功...\n" diff --git a/shell/check.sh b/shell/check.sh index efc13ea6..ba3c194f 100755 --- a/shell/check.sh +++ b/shell/check.sh @@ -38,7 +38,7 @@ pm2_log() { } check_nginx() { - local nginxPid=$(ps -ef | grep nginx | grep -v grep) + local nginxPid=$(ps -eo pid,command | grep nginx | grep -v grep) echo -e "=====> 检测nginx服务\n$nginxPid" if [[ $nginxPid ]]; then echo -e "\n=====> nginx服务正常\n"