I have the following command:
/usr/local/bin/forever start -o /home/username/path/out.log -e /home/username/path/err.log /usr/local/bin/nodemon --watch /home/username/scriptpath --exitcrash /home/username/scriptpath/example.js
Which I understand should:
Run Forever as a daemon
Run Nodemon, which will restart the script when a change is seen in /home/username/scriptpath, and will also 'exit' to forever on crashing, allowing forever to restart it all.
However I'm observing Nodemon not restarting upon changes to the files in the watched folder. (Though forever is restarting on crash, when I intentionally cause one.)
Note: Running only "nodemon example.js" works as expected, and restarts on change to file.
What do I need to change to allow Nodemon to re-start the script upon file changes?
My knowledge of linux commands are limited unfortunately, I may well be using the wrong ones.
It will only watch changes in files that are liked to the script you are running by require.
For instance: forever ./script.js ...
var x = require("./test")
Will restart ./script if ./test is changed.
I use forever-service and nodemon.
Here is an example of how I use it to do all you mention.
This example does the following: everytime a json or raml file in the applications dist/assets folder is modified, wait 10 seconds and then restart the node app (server.js script):
forever-service install raml --script server.js -f " -c nodemon" -o " --delay 10 --watch dist/assets -e json,raml --exitcrash" -e "PATH=/usr/local/bin:$PATH"
It will also dump a log file to /var/log/raml.log
I hope that helps!
Related
When I run a nodejs server with the following command:
"start": "nodemon --max-old-space=8192 ./src/app.js --exec babel-node"
and I change anything in the code, nodemon automatically reloads the code and restarts the server with the following message.
[nodemon] restarting due to changes...
[nodemon] starting `babel-node --max-old-space=8192 ./src/app.js`
How do I restart the server manually the same way?
Or in other words: What do I write in the package.json scripts "restart" command to simulate the same behaviour that is done by nodemon automatically?
Thanks
As stated in the documentation, you can restart manually by typeing rs in the console where nodemon is running.
There is no external command to trigger a restart from a different process.
One workaround would be to trigger a restart by simulating a change of a file.
A simple touch on a watched file is enough. So you could write a npm script that touches one of the watched files.
"restart": "touch app.js"
The purpose of nodemon is to listen changes of the file and restart the server. If you want manually to restart the server then you no need to use nodemon, you can use just node command.
The below code would serve the purpose.
{
"scripts": {
"start": "node ./src/app.js",
"restart": "kill -9 $(ps aux | grep '\snode\s' | awk '{print $2}') && node ./src/app.js "
},
}
Tried a few things to restart nodemon from within a running script.
var fs = require('fs');
fs.utimesSync(__filename, Date.now(), Date.now());
This will touch the current file, which should trigger a restart if nodemon is watching.
Say goodbye to nodemon.
Node v18.11.0
Running in 'watch' mode using node --watch restarts the process when an imported file is changed.
try as follow:
node --watch app.js
For more information
Source: https://www.npmjs.com/package/nodemon
Manual restarting
Whilst nodemon is running, if you need to manually restart your
application, instead of stopping and restart nodemon, you can type rs
with a carriage return, and nodemon will restart your process.
If you are specifically looking to resolve "listen EADDRINUSE: address already in use" error after a while, you can check what application is using the port that nodemon wants to use:
sudo lsof -i :4500
The above will give you the PID of the app that is using that port. Then you may kill the process by:
kill -9 <PID>
I'm trying to use nodemon with forever.
I have no problems with nodemon alone:
nodemon --exitcrash node/index.js -- "user/verbs/config"
However, in following the instructions in the nodemon FAQ, and putting quotes around "nodemon --exitcrash" as per the comment at https://stackoverflow.com/a/20306929/271577 (to avoid forever thinking the argument "user/verbs/config" is the file) to produce:
forever start --minUptime 1000 --spinSleepTime 1000 --killSignal=SIGTERM -c "nodemon --exitcrash" node/index.js -- "user/verbs/config"
...I get the message
info: Forever processing file: node/index.js
and no continuation of the script. Running forever list shows "No forever processes running".
(Note: I eventually want this working with forever-monitor, but I figure the above will need to work first.)
Is there something I'm missing?
forever -c "nodemon --exitcrash" app.js
this makes sure nodemon actually exits (rather than giving you the "app crashed" message) and then forever picks it up again.
In forever --help this -c specifies a command to run otherwise it defaults node. Without -c results in the error that is mention in the comments to this answer.
Source
Is there any way to have both of this packages running together?
So basically I want to have best from both worlds. Running server automatically (and restarting when there is an error) and also automatic updates when there is .js file change happening.
You should run something like this
forever start -c nodemon app.coffee
Toxa was on the right track, the issue that cfogelberg raised is valid, but to avoid that issue you can do the following:
forever -c "nodemon --exitcrash" app.js
this makes sure nodemon actually exits (rather than giving you the "app crashed" message) and then forever picks it up again.
In forever --help this -c specifies a command to run otherwise it defaults node. Without -c results in the error that is mention in the comments to this answer.
There is an entry about it in the nodemon FAQ:
If you're using nodemon with
forever (perhaps in a
production environment), you can combine the two together. This way if
the script crashes, forever restarts the script, and if there are file
changes, nodemon restarts your script. For more detail, see issue
30.
To achieve this you need to add the following on the call to
forever:
Use forever's -c nodemon option to tell forever to run nodemon instead of node.
Include the nodemon --exitcrash flag to ensure nodemon exits if the script crashes (or exits unexpectedly).
Tell forever to use SIGTERM instead of SIGKILL when requesting nodemon to stop. This ensures that nodemon can stop the watched node
process cleanly.
Optionally add the --uid parameter, adding a unique name for your process. In the example, the uid is set to foo.
bash forever start --uid foo --killSignal=SIGTERM -c nodemon
--exitcrash server.js
To test this, you can kill the server.js process and forever will
restart it. If you touch server.js nodemon will restart it.
To stop the process monitored by forever and nodemon, simply call the
following, using the uid we assigned above (foo):
bash forever stop foo
This will stop both nodemon and the node process it was monitoring.
Note that I would not recommend using nodemon in a production
environment - but that's because I wouldn't want it restart without my
explicit instruction.
I have not found a way of getting both packages running together. I tried to do #toxa's technique, but when my node.js app threw an exception nodemon would not automatically restart it, instead outputting an error message to the forever log:
nodemon] app crashed - waiting for file changes before starting...
However, forever has a -w option and the following command is effectively the same as if I'm running nodemon and forever together:
forever start -w my-app.js
The downside of forever -w versus nodemon: forever does not have a --delay option, so my server gets restarted once for each file that is changed.
I prefer a combo of what Toxa and Jubair suggest.
forever start -c nodemon app.coffee --exitcrash
If you need to pass arguments:
forever start -c "nodemon --harmony" app.js --exitcrash
I'm using forever-service . . .
This is what worked for me. It does the following: everytime a json or raml file in the applications dist/assets folder is modified, wait 10 seconds and then restart the node app (server.js script):
$ forever-service install raml --script server.js -f " -c nodemon" -o " --delay 10 --watch dist/assets -e json,raml --exitcrash" -e "PATH=/usr/local/bin:$PATH"
Then I can run:
$ service raml start|stop|restart|status
I can also have the service start on server reboot with the chkconfig utility:
$ chkconfig --add raml
$ chkconfig raml on
when using in the package.json use single quotes to make nodemon --existcrash as a single argument.
"start": "forever -c 'nodemon --exitcrash' server.js"
Output:
app_1 | [nodemon] app crashed
app_1 | error: Forever detected script exited with code: 1
app_1 | error: Script restart attempt #1
app_1 | [nodemon] 1.19.4
app_1 | [nodemon] to restart at any time, enterrs
app_1 | [nodemon] watching dir(s): *.*
app_1 | [nodemon] watching extensions: js,mjs,json
app_1 | [nodemon] startingnode /app/server.js`
app_1 | app is running on port 3000
`
Use like this: "start": "firever -c \"nodemon --exitcrash\" <main>.js"
Previously, I started my production node app via:
NODE_ENV=production forever start index.js
However, per the suggestions in this question, I'd like to start node with --nouse-idle-notification. I also found this article about setting --max-old-space-size, etc. Unfortunately, nobody I ask can seem to figure out how to tell if the flag is actually accepted by node, so I'm not sure how to tell if my forever syntax is correct.
Furthermore, I can't get forever to accept both arguments...
Eg, if I use this
NODE_ENV=production forever start --max-old-space-size=8192 --nouse-idle-notification index.js
I get the "forever usage information", as if I had tried to start forever without passing a .js file to run (eg, just typing "forever"). If I put the flags before the "start" command, it seems to start, but again I'm not sure how to tell if the flags were accepted...
Can someone please help me with the correct syntax?
You need to pass -c parameter:
forever start -c "node --max-old-space-size=8192 --nouse-idle-notification" index.js
If you list the processes, you'll see the flags are honoured.
forever list
Unless you really love forever for some other reason, try mon.
It's super easy to pass flags because you can specify the exact command:
mon "node --max-old-space-size=8192 --nouse-idle-notification --expose-gc server.js" -d
It monitors only a node process. If you want to monitor a group of processes like forever does, install mongroup, its a bash script that manages mon.
This will save you some RAM, specially if you're monitoring a lot of node processes (I think forever launches one additional node process for every process you want to monitor).
quick tip: last time I checked, TJ Holowaychuk's branch of mon was not working well under linux (I guess he only tested on Mac), but this one works and its the one I'm using right now. EDIT: Actually 2 days ago the issue was closed and the main branch should now be working.
You could try:
forever start --max-old-space-size=8192 --nouse-idle-notification -c "NODE_ENV=production node" index.js
My web app has 3 main node.js components: website, feeds and jobs.
To start these I am using forever:
//forever.js
var forever = require('forever');
function start(name){
forever.start( ['coffee', name + '.coffee'], { /* log options */ } )
};
start('website');
start('feeds');
start('jobs');
What I first noticed is that if I run script it wont run it as a daemon. ( Which is most likely normal )
node forever.js
So what I did next was run the forever.js script with forever. I am not sure if this is correct, there is also a forever.startDaemon so not sure which one I should use.
forever start forever.js
This works but the problem is that I would like to restart all the processes when a new version of my app is published. I am using git's post-receive hook to run the forever.js the first time but if I do this on each post-recieve it will just spawn 3 processes each time.
So I guess I need a way to restart 3 processes if they are already running. I thought to do this with forever.list but the documentation only say:
forever.list (format, callback)
Returns a list of metadata objects about each process that is being run using
forever. This method is synchronous and will return the list of metadata as such.
Only processes which have invoked forever.startServer() will be available from
forever.list()
First of all I am not sure what format means and second it expects a callback but then it says its synchronous. Which is a little confusing and I am not sure how to use list.
In the end all I want to do is start/restart 3 node.js processes on git's post-receive hook.
I think the best way to do this is:
forever start website.js
forever start feeds.js
forever start jobs.js
and then in your git post-receive hook:
forever restart website.js
forever restart feeds.js
forever restart jobs.js
Wrapping these node processes inside a single process is not a good idea. I now personally use Supervisord with monit instead of forever (supervisord is more stable & powerful than forever IMHO).
I do it like this:
#!/bin/sh
# Make sure we're in the right place
DIR=$(cd $(dirname "$0"); pwd)
cd $DIR
echo "[ I am $USER and I changed PWD to $DIR ]"
forever restart --spinSleepTime=2000 api_daemon.js || (forever start --spinSleepTime=2000 api_daemon.js && forever list)
Works like a charm, I never get duplicate processes using ./run.sh
To read logs, I use
tail -f /path/to/.log
Yes, it's possible. You need to use npm run forever command to run a script.
Add this to your package.json
"scripts": {
"forever" : "forever start api/api-server.js && forever start www/www-server.js && forever start upload/upload-server.js && forever start static/static-server.js",
"test": "echo \"Error: no test specified\" && exit 1"
}
You can create package.json using npm init