automatically restarting service via forever for nodejs

21,975

Solution 1

I personally use Nodemon to handle that. Its a replacement for the node server. It automatically restarts the server when your files are updated. You might want to check it out.

Solution 2

From the forever readme. Use the -w flag to watch file for changes.

Solution 3

In case someone else, like myself, comes to find this through google.

I have to run it thusly:

forever --watch ./start/file

For me, at least, it defaults to watching the current directory I'm running the command in for changes. ./start/file is the file that "npm start" hits up from your package.json.

If you need to watch a different directory from where you're pwd shows you to be, try:

forever --watch --watchDirectory ./path/to/dir ./start/file

For some reason "forever start xxxxxxxxx" only brings up the help information for me, but this works. /me shrugs.

Solution 4

Again just another example of its usage (and it does work :D)

forever -w --watchDirectory . --watchIgnore *.log -o ./log/out.log -e ./log/err.log index.js

That will launch the app in the same process with output to stdout/stderr (but also written to the logs)

To launch it in prod watching is obviously not a good idea and running it as a deamon is probably what you are after so drop the -w flags and add the "start" command

forever -o ./log/out.log -e ./log/err.log start index.js
Share:
21,975
coure2011
Author by

coure2011

Updated on July 09, 2022

Comments

  • coure2011
    coure2011 almost 2 years

    I found that forever can run nodejs server forever. Is forever supports this feautre?

    -- If the nodejs script is modified changed, the server shld restarted automatically.
    

    How can I enable this feature using forever? or I need something else?

  • coure2011
    coure2011 over 12 years
    how to use? forever start server.js -w
  • Rajat
    Rajat almost 12 years
    @DeaDEnD Can you give an example on how to watch the entire App directory?
  • AlessMascherpa
    AlessMascherpa about 11 years
    -w watches dir files and subdirs'
  • xandout
    xandout over 9 years
    thanks @CrimsonKissaki this was the key for my environment as well forever start --watch --watchDirectory ./ index.js
  • ericn
    ericn about 8 years
    this does not answer the question, forever definitely has this feature
  • Zeal Murapa
    Zeal Murapa almost 7 years
    nodemon is for development
  • user1709076
    user1709076 over 5 years
    forever --watch startme.js - makes it impossible for me to do 'forever stopall'. it says no processes running, but port 3000 is still occupied.