Nod.Js如何执行脚本,实时输出的实现方法是什么
Admin 2022-08-16 群英技术资讯 633 次浏览
需要一个服务来执行shell脚本,要求可以实时打印shell脚本执行的过程,并看到脚本执行的结果。
这是一个web服务,需要执行shell脚本
当一个脚本执行的时候,再次发送请求需要等待当前脚本执行完毕,再自动执行这次请求
使用长连接而不是socket
添加脚本不需要重启服务器
这里采用的是express框架
新建app.js
文件, npm install express
const express = require('express');
const app = express();
app.get('/:id', (req, res) => {
const { id } = req.params;
if (id === 'favicon.ico') {
res.sendStatus(200);
return;
}
// 执行脚本
});
app.set('port', 3018);
app.listen(app.get('port'), () => console.log(`server listening at ${app.get('port')}`));
新建config.json
用于配置id和脚本名的对应关系,新建scripts
目录用于存放脚本。
这里定义一个函数execute
参数为id和response对象,代码如下:
const pidDict = {};
async function execute(id, res) {
delete require.cache[require.resolve('./config.json')];
const config = require('./config.json');
const filePath = config[id];
if (!filePath) {
res.sendStatus(404);
return;
}
console.log(`The script:${filePath} with ${id} begin execute`);
const readable = new Readable();
readable._read = () => {};
readable.pipe(res);
while (pidDict[id]) {
readable.push('\nWaiting for another script request.');
await wait(5000);
}
const handle = spawn('sh', [`./scripts/${filePath}`]);
pidDict[id] = handle.pid;
handle.stdout.on('data', (data) => {
readable.push(`\n${data}`);
getLogger(filePath).log(`\n${data}`);
});
handle.stderr.on('data', (data) => {
getLogger(filePath).warn(`\n${data}`);
readable.push(`\n${data}`);
});
handle.on('error', (code) => {
getLogger(filePath).error(`child process error with information: \n${code}`);
readable.push(`child process error with information: \n${code}`);
delete pidDict[id];
readable.push(null);
});
handle.on('close', (code) => {
getLogger(filePath).log(`child process close with code ${code}`);
delete pidDict[id];
readable.push(null);
});
}
解释:
首先要加载config.json
,需要注意的是,因为是需要动态引入,所以这里不能直接使用require('config.json')
,在这之前,需要先删除引入的缓存:delete require.cache[require.resolve('./config.json')];
获取文件路径 const filePath = config[id];
新建读写流,可以直接发送到前端。
再执行脚本前,需要判断当前有无脚本执行,这里在外部定义了一个pidDict,文件对应的id直接指向文件执行的handle的pid
紧接着就是输入输出流了
handle.stdout是标准输出
handle.stderr是错误输出,这里指的是输出的警告
handle的error事件指的是脚本执行中遇到的错误,也就是脚本执行不成功报的错误信息
这里定义了两个外部函数,一个是自定义的日志打印,另一个是遇到有脚本执行时的等待
新建utility.js
const fs = require('fs');
/**
* time wait
*
* @param time {number} time(ms) to wait
*/
/* eslint-disable compat/compat */
const wait = async (time = 1000) => {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
};
/**
* set log
*
* getLogger(path).level
* level:
* log
* trace
* debug
* info
* warn
* error
* @param path
*/
function getLogger(path) {
return require('tracer').console({
transport: (data) => {
console.log(data.output);
fs.appendFile(`./logs/${path}.log`, `${data.rawoutput} \n`, () => {});
},
});
}
module.exports = {
wait,
getLogger,
};
现在,新建scripts/hello-world.sh
#!/bin/sh
echo 'hello...'
sleep 5
echo 'world!'
config.json中注册该脚本
{
"hello-world": "hello-world.sh"
}
执行node app.js
,通过curl http://localhost:3018/hello-world
即可观察到运行结果。
这里放上app.js的完整代码
const express = require('express');
const { spawn } = require('child_process');
const { Readable } = require('stream');
const { wait, getLogger } = require('./utility');
const app = express();
app.get('/:id', (req, res) => {
// 执行脚本
const { id } = req.params;
if (id === 'favicon.ico') {
res.sendStatus(200);
return;
}
execute(id, res).then();
});
const pidDict = {};
/**
* 执行sh脚本
*
* @param id 脚本id
* @param res response object
*/
/* eslint-disable no-underscore-dangle, no-await-in-loop */
async function execute(id, res) {
delete require.cache[require.resolve('./config.json')];
const config = require('./config.json');
const filePath = config[id];
if (!filePath) {
res.sendStatus(404);
return;
}
console.log(`The script:${filePath} with ${id} begin execute`);
const readable = new Readable();
readable._read = () => {};
readable.pipe(res);
while (pidDict[id]) {
readable.push('\nWaiting for another script request.');
await wait(5000);
}
const handle = spawn('sh', [`./scripts/${filePath}`]);
pidDict[id] = handle.pid;
handle.stdout.on('data', (data) => {
readable.push(`\n${data}`);
getLogger(filePath).log(`\n${data}`);
});
handle.stderr.on('data', (data) => {
getLogger(filePath).warn(`\n${data}`);
readable.push(`\n${data}`);
});
handle.on('error', (code) => {
getLogger(filePath).error(`child process error with information: \n${code}`);
readable.push(`child process error with information: \n${code}`);
delete pidDict[id];
readable.push(null);
});
handle.on('close', (code) => {
getLogger(filePath).log(`child process close with code ${code}`);
delete pidDict[id];
readable.push(null);
});
}
app.set('port', 3018);
app.listen(app.get('port'), () => console.log(`server listening at ${app.get('port')}`));
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
今天我们来了解一下vuex,下文会给大家分享vuex中四大对象state、mutations、getters、actions以及他们的用法,对新手学习和理解vuex对象有一定的帮助,感兴趣的朋友就继续往下看吧。
这篇文章主要为大家详细介绍了jQuery实现表单验证功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
1、利用a标签的href属性添加文件URL,语法“下载”。2、使用url跳转下载,语法“window.open(文件url)”。
这篇文章主要介绍了JavaScript数组 几个常用方法,主要概述的方法有filter()、map()、sort()、reduce()、forEach(),这些方法都是JavaScript常用到的方法,下面文章内容详细介绍了他们的语法、参数、返回值等资料,需要的朋友可以参考一下
本文详细介绍了从vue的组件传值着手观察者模式,感兴趣的同学,可以参考下,理解其原理。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008