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 cleanly exit nodemon #751

Closed
edwardhorsford opened this issue Jan 11, 2016 · 19 comments
Closed

How to cleanly exit nodemon #751

edwardhorsford opened this issue Jan 11, 2016 · 19 comments
Labels
stale no activity for 2 weeks

Comments

@edwardhorsford
Copy link

We have a situation where we would like to completely quit node and nodemon.

We are using process.exit(0) to cleanly exit node. When this happens, we also want nodemon to quit. Instead it stays running and monitoring the directory.

How do we cleanly exit nodemon from within the app?

@nerdpad
Copy link

nerdpad commented Mar 7, 2016

+1

Would like to know this as well.

@dundalek
Copy link

I was looking for this too. I found a workaround that might be helpful.

If you exit with process.exit(2) it will cause nodemon also to exit. Nodemon will print stack trace so you might also hook into error event to make it clean, e.g.:

nodemon(...)
  .on('error', function(code) {
    if (code !== 2) {
      console.log('Exited with error code: ', code);
    }
  });

if you don't want to or can't modify the child process it should be possible to run it like your_command || exit 2 to make it work.

More details: #627

@stevenvachon
Copy link

In tests, I cannot fully kill a nodemon process. emit('quit') only triggers the listener(s) and:

nodemon(...).on('quit', function(){ this.reset() })

causes:

Assertion failed: (0), function uv_close, file ../deps/uv/src/unix/core.c, line 173.

@stale
Copy link

stale bot commented Dec 5, 2017

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@stale stale bot added the stale no activity for 2 weeks label Dec 5, 2017
@stale stale bot closed this as completed Dec 12, 2017
@joeytwiddle
Copy link
Contributor

joeytwiddle commented Jul 10, 2018

There has been some debate about removing the quit behaviour on exit code 2.

So I would not recommend this technique, if you can find an alternative way.

Edit: We actually did remove it!

@OoDeLally
Copy link

What is the state of this?
When process.exit(2) I get

[nodemon] app crashed - waiting for file changes before starting...

then I have to Ctrl+C a second time to actually kill it.

1- Is it the expected behavior?
if yes, is a app crashed red message really appropriate? Ctrl+C is not a "crash" per se.
2- Any way to nicely quit everything in one key stroke (not twice) and without a scary crash message?
Thanks!

@remy
Copy link
Owner

remy commented Dec 9, 2019

@OoDeLally exit(2) is a crash code (from your script), so if you're catching the exit code inside your program and using exit 2, then nodemon is behaving exactly as it should.

A clean exit code is 0, anything above is a bad exit code, and nodemon will catch it.

Also, this issue was raised 3 years ago, so is unrelated to your original question.

@OoDeLally
Copy link

OoDeLally commented Dec 9, 2019

Thank you @remy. process.exit(0) indeed nicely quits the nodeJS process, but not nodemon.

Is there a way to gracefully quit both nodejs and nodemon at once with Ctrl+C, given that I run nodemon with --no-stdin?
I am able to capture Ctrl+C in nodeJS, but after that, I am unable to gracefully quit both.

If I understood @edwardhorsford right, this is exactly what he originally asked, so that is why I am posting here.

Thank you

@remy
Copy link
Owner

remy commented Dec 10, 2019 via email

@edwardhorsford
Copy link
Author

@remy it's some years since I raised this (and I no longer work for that bit of gov that used it).

My use case was where the app's code has determined that nodemon should be quit. How should the app go about quitting nodemon? I think we had a situation where we asked the user a question in the terminal, and if they responded negatively, we then wanted to fully quit - at the time, we couldn't work out how to quit nodemon programatically.

@remy
Copy link
Owner

remy commented Dec 10, 2019

Darn, I didn't realise I was commenting on a 3 year old issue (I was doing it via email).

Bottom line: unless you require nodemon as a dep, nodemon will not exit. This is expected behaviour.

@BobFrankston
Copy link

It may be an old issue but one that is still relevant. I run a number of daemons using nodemon and they don't have a command line. When I run directly under node I can tell the program to exit itself but how do I do that with nodemon?

@joeytwiddle
Copy link
Contributor

joeytwiddle commented Dec 8, 2020

I think the neatest way to implement this might be to let callers opt in to an exit code that would cause nodemon to quit. For example:

nodemon --kill-code 222 ...

Would cause nodemon to close if the child process exits with code 222.

(An alternative, more readable option name might be --die-on 222 but -d is already taken, as are -e and -x)

@remy
Copy link
Owner

remy commented Dec 8, 2020

Firstly you're commenting on an issue from 4 years ago (and closed).

Secondly, you can use the OS to do this without having to add to nodemon:

nodemon || exit 222

@BobFrankston
Copy link

I know it's an old issue. But it's not clear that it has been resolved. Note that I'm using Windows.

In the meantime I've taken a different approach that doesn't depend on nodemon.

@heymartinadams
Copy link

heymartinadams commented Dec 22, 2020

It seems strange to me that nodemon doesn’t provide a clean, programmatic way to exit itself (in case it does, I can’t find it in the documentation).

I’ve since been using babel-node with better results.

@Poikilos
Copy link

Poikilos commented Dec 23, 2020

Ok, what's the "..." guys? I can't seem to get this done using:

nodemon = require("nodemon");

  nodemon()
  .on('error', function(code) {
    if (code !== 2) {
      console.log('Exited with error code: ', code);
      process.exit(code)
    }
  });

process.exit(99)

I get:

/home/owner/git/node-ticket-manager/node_modules/nodemon/lib/nodemon.js:37
  if (settings.verbose) {
               ^

TypeError: Cannot read property 'verbose' of undefined

The surrounding code is lib/server.js in: https://github.com/poikilos/node-ticket-manager

@molten-firescar96
Copy link

Starting the main script via a proxy seems to address the original question, how to make sure nodemon ends when the child process ends.

// run-main.mjs
import nodemon from 'nodemon';

const mainArgs = process.argv.slice(2).join(' ');
nodemon(`<nodemon arguments> extensions/main.mjs ${mainArgs}`);
let ignoreExit = false;
nodemon.on('restart', () => {
  ignoreExit = true;
}).on('exit', () => {
  if(ignoreExit) {
    ignoreExit = false;
    return;
  };
  process.exit();
});

@mprast
Copy link

mprast commented Sep 5, 2022

For anyone who stumbles on this thread in the future - there now appears to be an --exitcrash flag which will cause nodemon to exit if the child process crashes.

cas-- added a commit to cas--/traefik-avahi-helper that referenced this issue Feb 16, 2023
Unsure the exact reason but need to catch the exit event and call exit
to ensure nodemon doesn't hang.

Use a workaround as suggested in: remy/nodemon#751
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale no activity for 2 weeks
Projects
None yet
Development

No branches or pull requests