Skip to content
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

How to restart node app from iteself? #923

Closed
ronovar opened this issue Oct 25, 2017 · 12 comments
Closed

How to restart node app from iteself? #923

ronovar opened this issue Oct 25, 2017 · 12 comments

Comments

@ronovar
Copy link

ronovar commented Oct 25, 2017

I need when i call api for /restart to restart application.js i try using this code:

child_process.exec(['killall -9 node && node --expose-gc /root/application.js'], 
                   function (error, stdout, stderr) { }
):

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

@Knighton910
Copy link
Contributor

Have you tried nodemon: https://nodemon.io/

@ronovar
Copy link
Author

ronovar commented Oct 26, 2017

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?

@MM422
Copy link

MM422 commented Oct 27, 2017

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

@ORESoftware
Copy link

ORESoftware commented Oct 27, 2017

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:

https://medium.com/@the1mills/diy-nodemon-5176fdcc1cfb

@ronovar
Copy link
Author

ronovar commented Oct 28, 2017

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

@gireeshpunathil
Copy link
Member

@ronovar - is this still outstanding?

@gireeshpunathil
Copy link
Member

closing due to inactivity, please re-open if it is still outstanding.

@rockyjvec
Copy link

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.

@twome
Copy link

twome commented Mar 27, 2019

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()
}

@ChiweenieDijon
Copy link

Just an FYI in case others seeking possible solutions come across this thread, here's a potentially useful mechanism:
If you are running under a process manager w/ auto-restart, (e.g. PM2),
You can send yourself a sigint, or sigterm:

process.kill(process.pid, 'SIGINT')
or
process.kill(process.pid, 'SIGTERM')

(although i think it's not as simple on windows)

@LingleDev
Copy link

LingleDev commented Sep 8, 2021

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>")

@true-goniss
Copy link

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):

node script.js
pause

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants