标签:end 命令 运行 信号 并且 exception ecs proc err
#!/bin/env node
console.log (process.execPath)
console.log (process.cwd())
console.log (process.pid)
let user = process.env.USER || ""
console.log (user)
process.on(‘exit‘, function () {
console.log(‘Bye.‘);
});
process.on(‘uncaughtException‘, function (err) {
console.log(‘Caught exception: ‘ + err);
});
const { spawn } = require(‘child_process‘);
const ps = spawn(‘ps‘, [‘ax‘]);
const grep = spawn(‘grep‘, [‘ssh‘]);
ps.stdout.on(‘data‘, (data) => {
grep.stdin.write(data);
});
ps.stderr.on(‘data‘, (data) => {
console.error(`ps 的 stderr: ${data}`);
});
ps.on(‘close‘, (code) => {
if (code !== 0) {
console.log(`ps 进程退出,退出码 ${code}`);
}
grep.stdin.end();
});
grep.stdout.on(‘data‘, (data) => {
console.log(data.toString());
});
grep.stderr.on(‘data‘, (data) => {
console.error(`grep 的 stderr: ${data}`);
});
grep.on(‘close‘, (code) => {
if (code !== 0) {
console.log(`grep 进程退出,退出码 ${code}`);
}
});
//-------------------------------------
const { spawn } = require(‘child_process‘);
const grep = spawn(‘grep‘, [‘ssh‘]);
grep.on(‘close‘, (code, signal) => {
console.log(`子进程因收到信号 ${signal} 而终止`);
});
// 发送 SIGHUP 到进程。
grep.kill(‘SIGHUP‘);
标签:end 命令 运行 信号 并且 exception ecs proc err
原文地址:https://www.cnblogs.com/itech/p/13253278.html