问题
想用 websocketd 这个工具,将脚本的输出打印在浏览器上。
但是在脚本中的 git, npm 等等程序的输出,却不会被 websocketd 读取到。
代码重现
脚本 script.sh:
#!/bin/bash
echo 'start'
echo 'git'
git clone git@github.com:necolas/normalize.css.git
echo 'npm'
npm i normalize.css
echo 'finish'
前端js:
const ws = new WebSocket('ws://localhost:8080/');
ws.onopen = function() {
console.log('connect');
};
ws.onmessage = function(event) {
console.log(event.data);
};
ws.onclose = function () {
console.log('disconnect');
};
ws.onerror = function(event) {
console.error(event);
}
websocketd命令:
websocketd --port=8080 sh script.sh
结果重现
connect
start
git
npm
finish
disconnect
期待结果
将 git, npm 程序原本能在控制台输出的内容输出到 websocketd
我在 https://cloud.tencent.com/dev... 和 https://www.kutu66.com//ubunt... 找到答案了。
原来 git, npm 等它们的输出使用的是 stderr,需要重定向到 stdout,才能被 websocketd 捕捉到。所以可以这样做: