nodejs新进程子进程
2021-01-24 14:14
标签:end 命令 运行 信号 并且 exception ecs proc err nodejs新进程子进程 标签:end 命令 运行 信号 并且 exception ecs proc err 原文地址:https://www.cnblogs.com/itech/p/13253278.html获取进程相关的基本信息
#!/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);
});
启动进程的各种方法的比较
以非常精细的方式运行 ps ax | grep ssh
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‘);
上一篇:One Network To Segment Them All: A General, Lightweight System for Accurate 3D Medical Image Segment
下一篇:supersocket client https://www.cnblogs.com/franklin2018/p/11494747.html