-
Notifications
You must be signed in to change notification settings - Fork 293
How to restart node app from iteself? #923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Have you tried nodemon: https://nodemon.io/ |
I need to restart it internaly from itself...i have app with buttons restart stop and shutdown...so when user click stop if i use nodemon it will restart not kill app....so do you have idea how can i use to restart internally using javascript code? |
Hi, I am not quite sure whether I have fully understand about your use case but could you try this: https://github.com/foreverjs/forever |
Best thing to do is to launch a process which launches your app as a child process. The parent process can kill and restart the child process at will. I wrote a simple nodemon alternative, the source is here: |
Thanks ORESoftware...but i have node application that check if node isMaster process and i need to restart master process not child process..because i do node process clustering and i im using node clustering to do heavy stuff there so master process is what i need to restart using console...i read that best aproach is using terminal command like above but not using child process and using spawn but i try few spawn commands but none works |
@ronovar - is this still outstanding? |
closing due to inactivity, please re-open if it is still outstanding. |
I thought I'd share how I did this in hopes it might help someone else searching how to do the same thing. This issue was the first result on google when I was searching for how to do this. I'm using rabbitmq to manage scalable puppeteer worker processes for PDF generation of HTML pages. I'm using a rabbitmq exchange to send messages to all the workers to restart or shutdown themselves. Here is the relevant code I am using to have the worker processes restart themselves: const {spawn} = require('child_process');
if(action === 'restart')
{
console.log("restarting...");
const logfile = 'path_to_the_log_file.log';
const out = fs.openSync(logfile, 'a');
const err = fs.openSync(logfile, 'a');
const subprocess = spawn(process.argv[1], process.argv.slice(2), {detached: true, stdio: ['ignore', out, err]});
subprocess.unref();
}
else
{
console.log("shutting down...");
}
process.exit(); Depending on how you originally start the process, you may need to change argv[1] and slice(2) to argv[0] and slice(1). Hope that helps. |
Single-statement version of @rockyjvec's incredibly useful answer: const restartProcess = () => {
spawn(process.argv[1], process.argv.slice(2), {
detached: true,
stdio: ['ignore', out, err]
}).unref()
process.exit()
} |
Just an FYI in case others seeking possible solutions come across this thread, here's a potentially useful mechanism:
|
If you're using pm2, you could simply run this bit of code to restart your process: var { exec } = require('child_process')
exec("pm2 restart <yourprocessname>") |
Solution for windows (none of the posted solutions worked): create restart_script.bat at the same folder as the script with the following content (replace the name):
then you can restart the script from itself: const wait = async(ms) => { return new Promise(resolve => setTimeout(resolve, ms)); }
const { spawn } = require('child_process');
const bat = spawn('restart_script.bat', [], {
shell: true,
stdio: 'inherit'
});
await wait(500);
process.exit(1); |
I need when i call api for /restart to restart application.js i try using this code:
But the problem is that node is killed but application.js is not run and application is not running.
I read that i need to use spawn not child_process.exec but i im unable to make it work...so can you please post me above code of using spawn?
I im using node version: v7.10.1
ubuntu server x64 14.04 LTS
I need to kill node (application.js) and run it again...so short say restart application.js
Thanks
The text was updated successfully, but these errors were encountered: