child_process.exec(command[, options][, callback])

  1. const{exec}=require("child_process")
  2. /**
  3. * 移动文件
  4. */
  5. // exec("mv ./1.txt ./a ",{
  6. // cwd:__dirname
  7. // },(error)=>{
  8. // console.log(error)
  9. // })
  10. /**
  11. * 删除文件
  12. */
  13. // exec('rm ./a.txt')
  14. /**
  15. * 复制文件目录
  16. */
  17. // exec('cp -r ./1 ./b',{
  18. // cwd:__dirname
  19. // },(error)=>{
  20. // console.log(error)
  21. // })

exec 守护

  1. const {exec}=require("child_process")
  2. let serverList=['server1','server2']
  3. serverList.forEach(serverName=>{
  4. server(`node ./${serverName}.js`)
  5. })
  6. function server(cmd){
  7. const worker=exec(cmd,(error,stdout,stderr)=>{
  8. if(error)return console.error(error)
  9. })
  10. worker.stdout.on('data',data=>{
  11. console.log(data)
  12. })
  13. worker.on("exit",(code)=>{
  14. if(code===1){
  15. setTimeout(()=>{
  16. server(cmd)
  17. },3000)
  18. }
  19. })
  20. }

spawn

  1. const{spawn}=require("child_process");
  2. /**
  3. * 当前目录文件名
  4. */
  5. // const showInfo=spawn('ls')
  6. // showInfo.stdout.on("data",(data)=>{
  7. // console.log(data.toString())
  8. // })
  9. // showInfo.stderr.on("data",(data)=>{
  10. // console.log(`stderr:${data}`)
  11. // })
  12. // showInfo.on("close",(code)=>{
  13. // console.log(`子进程退出,退出码${code}`)
  14. // })

spawn 守护

  1. const {spawn}=require("child_process");
  2. let serverList=['server1','server2']
  3. serverList.forEach((serverName=>{
  4. server(`./${serverName}.js`)
  5. }))
  6. function server(cmd){
  7. const worker=spawn('node',[cmd])
  8. worker.stdout.on('data',data=>{
  9. console.log(data.toString())
  10. })
  11. worker.stderr.on("data",data=>{
  12. console.log(data.toString())
  13. })
  14. worker.on("exit",code=>{
  15. if(code===1){
  16. setTimeout(()=>{
  17. server(cmd)
  18. },3000)
  19. }
  20. })
  21. }

child_process.execFile(file[, args][, options][, callback])

  1. const {promisify}=require("util");
  2. const {exec,execFile,spawn,fork}=require("child_process")
  3. const _execFile=promisify(require("child_process").execFile)
  4. // _execFile('node',['--vision']).then(res=>{
  5. // console.log(res.stdout)
  6. // }).catch(err=>{
  7. // console.log(err)
  8. // })

execFile 守护

  1. const { execFile } = require("child_process")
  2. let serverList = ['server1', 'server2']
  3. serverList.forEach((serverName => {
  4. server(`./${serverName}.js`)
  5. }))
  6. function server(cmd) {
  7. const worker = execFile('node', [cmd],(error,stdout,stderr)=>{
  8. if(error)return console.log(error)
  9. })
  10. worker.stdout.on('data', data => {
  11. console.log(data)
  12. })
  13. worker.on("exit", (code) => {
  14. console.log(code)
  15. if (code === 1) {
  16. setTimeout(() => {
  17. server(cmd)
  18. }, 3000)
  19. }
  20. })
  21. }

child_process.fork(modulePath[, args][, options])

  1. const {work}=require("child_process") //从主进程中分离出的一个独立的子进程
  2. function server(cmd){
  3. const worker=fork([cmd])
  4. worker.on("data",data=>{
  5. console.log(datastdout.toString())
  6. })
  7. }
  8. let serverList=['server1','server2']
  9. serverList.forEach(serverName=>{
  10. server(`./${serverName}.js`)
  11. })

fork 守护

  1. const {fork} =require("child_process")
  2. let serverList=['server1','server2']
  3. serverList.forEach(serverName=>{
  4. server(`./${serverName}.js`)
  5. })
  6. function server(cmd){
  7. const worker=fork([cmd])
  8. worker.on("data",(data)=>{
  9. console.log(data.stderr)
  10. })
  11. worker.on("exit",(code)=>{
  12. if(code===1){
  13. setTimeout(()=>{
  14. server(cmd)
  15. },3000)
  16. }
  17. })
  18. }