Skip to content

How to restart node app from iteself? #923

Closed

Description

@ronovar

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

Activity

  1. Knighton910 commented on Oct 26, 2017

    @Knighton910
    Contributor

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

  2. ronovar commented on Oct 26, 2017

    @ronovar
    Author

    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?

  3. MM422 commented on Oct 27, 2017

    @MM422

    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

  4. ORESoftware commented on Oct 27, 2017

    @ORESoftware

    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

  5. ronovar commented on Oct 28, 2017

    @ronovar
    Author

    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

  6. gireeshpunathil commented on Mar 26, 2018

    @gireeshpunathil
    Member

    @ronovar - is this still outstanding?

  7. gireeshpunathil commented on Mar 27, 2018

    @gireeshpunathil
    Member

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

  8. rockyjvec commented on Sep 24, 2018

    @rockyjvec

    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.

  9. twome commented on Mar 27, 2019

    @twome

    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()
    }
  10. ChiweenieDijon commented on Nov 20, 2019

    @ChiweenieDijon

    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)

  11. LingleDev commented on Sep 8, 2021

    @LingleDev

    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>")
  12. true-goniss commented on May 3, 2024

    @true-goniss

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions