Last active
September 2, 2024 09:14
-
-
Save silverwind/d0802f7a919ae86ff25e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
"use strict"; | |
console.log("This is pid " + process.pid); | |
setTimeout(function () { | |
process.on("exit", function () { | |
require("child_process").spawn(process.argv.shift(), process.argv, { | |
cwd: process.cwd(), | |
detached : true, | |
stdio: "inherit" | |
}); | |
}); | |
process.exit(); | |
}, 5000); |
I wonder if there could be a way to restart the process and have it still accept user input properly. It seems a bit sloppy to leave dead processes around doing nothing.
Only this one solution allow helped me to prevent memory leak in nodejs, argh! Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was so helpful! It got me 99% of what I needed. I want to just comment my use case and how it differed a little bit in case it helps someone else. My script was to be run in the terminal and get input from the user. If they chose a certain option, I would do some work, and then need to restart the script so the work would be reflected correctly, but it would need to continue getting user input on the restarted process (which was the tricky part for me to get right).
It (getting user input from keyboard) finally started working as expected when I DIDN'T explicitly do a
process.exit()
after spawning the child process.