2

Thanks you for clicking on this question. I am trying to restart a nodeJS process, without external libraries. I did this code:

function restartProcess() {
spawn(process.argv[1], process.argv.slice(2), {
  detached: true, 
  stdio: ['ignore', out, err]
}).unref()
process.exit()
}
bot.on( "kicked", (reason) => {
    console.log("KICKED! "+ reason)
    restartProcess()
})

But, when restartProcess() is called, i get error:

C:\Users\Toshiba\Desktop\program\mineflayer\spbot\bot.js:7
      stdio: ['ignore', out, err]
                        ^

ReferenceError: out is not defined
    at restartProcess (C:\Users\Toshiba\Desktop\program\mineflayer\spbot\bot.js:7:25)
    at EventEmitter.<anonymous> (bot.js:54:5)
    at EventEmitter.emit (events.js:315:20)
    at Client.<anonymous> (C:\Users\Toshiba\node_modules\mineflayer\lib\plugins\kick.js:5:9)
    at Client.emit (events.js:315:20)
    at FullPacketParser.<anonymous> (C:\Users\Toshiba\node_modules\minecraft-protocol\src\client.js:89:12)
    at FullPacketParser.emit (events.js:315:20)
    at addChunk (C:\Users\Toshiba\node_modules\readable-stream\lib\_stream_readable.js:298:12)
    at readableAddChunk (C:\Users\Toshiba\node_modules\readable-stream\lib\_stream_readable.js:280:11)
    at FullPacketParser.Readable.push (C:\Users\Toshiba\node_modules\readable-stream\lib\_stream_readable.js:241:10)

Any help would be VERY appreciated!

XXfedeXX
  • 81
  • 1
  • 8
  • 1
    well, at least in the code we see `out` and `err` are indeed not defined. Did you mean `process.stdout` and `process.stderr`? I think so. But I'm not sure what you are trying to do here will work. I think you should wrap your current process (from the outside) in something like the npm `forever` tool. – Christian Fritz Jan 08 '21 at 14:59
  • I don't think you can make a process restart itself. You need an external manager to restart it, like nodemon (dev) or PM2 (prod/persistent) – Jeremy Thille Jan 08 '21 at 15:15
  • This was answered here https://stackoverflow.com/questions/40835187/node-js-process-restart, but generally speaking, using something like monit, PM2, etc., would be the "correct" way to do this. You could have `bot.on('kicked', () => process.exit(0))`, and then the process manager would restart for you. You would also likely need to use `process.argv` starting at `0` (the Node executable). – Zac Anger Jan 08 '21 at 15:56

1 Answers1

0

Change to:

function restartProcess() {
spawn(process.argv[1], process.argv.slice(2), {
  detached: true, 
  stdio: ['ignore', process.stdout, process.stderr]
}).unref()
Or Assayag
  • 5,662
  • 13
  • 57
  • 93