How to get forever to print child process output to stdout. - node.js

I'm running node app under forever in a docker container, how do I get all console.log output to show up on the console? I'm a beginner to docker so I assume the best way to log with docker is to simply log to stdout. If there's a better more proper way to do this I'm open to alternative solutions.

forever, like standard node, simply logs to stdout as well so you shouldn't have to do anything special. Of course, if you start your container with docker run -d you'll have to run docker logs -f myNodeContainer to actually see the output live.

I just came across this question because I also installed forever for the first time, ran it with just forever server.js and...nothing.
No node console messages, just the warnings about no --minUptime and no --spinSleepTime. I stopped the forever processes using forever stopall and went looking for the logs. Sure enough, in the forever log was all the stdout from my server.
I assumed I was missing an option to print to stdout, but apparently not. After reading this SO question, I launch a fresh terminal, run it again and it works - it now logs my server messages to the console.
Since then I have also experienced lots of strange issues with forever. Like it restarting the script continuously, even though the script fails to start within 1000ms (there was a syntax error in one of the files) - I stop it, run forever again (same options, same source files) and it does what it's supposed to (only runs the script once).
Not really an answer (sorry), but this is just in case anyone comes seeking information.

Related

How to get logs of NodeJs application running in forever npm?

I was wondering, is there any way I can check out the logs of the my nodejs server when I've forever'd it using forever start server.js to look at what's being logged, check any live errors and such.
I looked at their documentation but couldn't find anything related to this. I want to be able to look at the console.
As forever start an application on background it gets hard to access old logs.
If you can use a library for maintaining logs, I will suggest simple-node-logger . Using this library you can save all your logs on a file and can access that file any time using:
tail -f logsFile.txt
Using this command this will keep printing new changes over logsFile.txt as logger keep updating logs.
This will help you to maintain all the logs for you.
First find out where your logs are stored using:
sudo forever list
for each process you can see something like this as logfile:
/root/.forever/_Jht.log
use 'tail -f' to monitor its changes:
sudo tail -f /root/.forever/_Jht.log
You can also set your desired path for log files, see this:
https://stackoverflow.com/a/21021251/1119611

Nodemon server perpetuality and runtime log issue

I have a sailsjs app on AWS EC2, which I have been running till now using forever. I have two adantages using forever:
1) Perpetuality: I can use the CLI forever start app.js or forever restart app.js and then app starts running and keeps on running till I stop it with the command forever stop app.js. So, the app does not stop even when I close my terminal. The process keeps on running.
2) Runtime Log: I have a .forever directory that has a log file, while on real time records the server logs, and when I check the log using tail -f file_name.log, I get to see run time logs.
However there is a disadvantage: Every time I upload a new/modified server file, I have to restart the app manually. To get rid of this, I am switching from forever to nodemon.
From the documentation provided by Nodemon, I cant figure out how can I replicate the two advantages, as mentioned above, from Nodemon too. Will be a great help if anyone can guide me on how to start my nodejs app using nodemon so that it can keep running even after closing the terminal on my side, and how to watch runtime log of server.
Just my two cents.
I use nodemon daily while developing and I dont think its something you want to use in place of something like forever. Nodemon is used when developing, the software will detect when there has been a file change and restart the server but for deployment it should not be considered.
There is no need to change either because forever has this use case handled with the --w or --watchDirectory comand that will watch for file changes(It can be found here on their readme).

What is forever in nodejs?

I am having hard time understanding what is forever in nodejs.
Can someone explain what is forever in simplest way that i can understand and what is the purpose of it
forever is a node.js package that is used to keep the server alive even when the server crash/stops. When the node server is stopped because of some error, exception, etc. forever automatically restarts it
From the npmjs https://www.npmjs.com/package/forever
A simple CLI tool for ensuring that a given node script runs continuously (i.e. forever)
Forever can be used as
forever start app.js
It provides many useful functions which you can see in the link above.
Directly quoting from http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever/
The purpose of Forever is to keep a child process (such as your node.js web server) running continuously and automatically restart it when it exits unexpectedly.
Forever basically allows you to run your nodejs application as a process.
Without forever you might type npm start or node index.js to start your application and it will run in your terminal session.
With forever, you can start it off and still have access to your terminal session/close it.
You can even use the nohup command it simply ignores the hang up signal.
node index.js && nohup -& (to run as a background process - no hiccup)

keep server alive after closing command prompt (with forever or forever-monitor)

I'm using Amazon WS to test some rudimentary nodejs server. The problem I'm having is that when I close the putty command prompt on my PC, that I can not reach the server anymore with a browser.
Ive read about forever and forever-monitor. I'n not sure why the script must be restarted constantly, but ok let's assume it must.
I'm using both
forever "/home/ec2-user/myApp.js"
and
node "/home/ec2-user/foreverMonitor.js"
(The latter has the myApp.js reference in the foreverMonitor.js file. Similar to Where place forever-monitor code?.)
Both do start the server, but when I close putty, both also let the server die.
What am I missing here?
------------------------------------- update -------------------------------------
I guess I can also skip foreverMonitor (not verified yet)
nohup forever "/home/ec2-user/myApp.js" &
forever stop "/home/ec2-user/myApp.js"
------------------------------------- update -------------------------------------
working, now using this
nohup forever "/home/ec2-user/foreverMonitor.js" &
forever stop "/home/ec2-user/foreverMonitor.js"
I'm not totally familiar with AWS, but it seems that you probably need to run nohup. The trailing ampersand should give you control of the terminal again immediately after executing the command.
$ nohup forever "/home/ec2-user/myApp.js" &
$ nohup node "/home/ec2-user/foreverMonitor.js" &
See this answer for more details on nohup and the trailing ampersand: https://stackoverflow.com/a/15595391/498624
Have a look at PM2 https://github.com/Unitech/pm2
After using forever successfully, I switched to PM2.
forever works fine but I found PM2 was a better fit to my mental model. PM2 also has a very neat (and repidly evolving) Web interface where you can monitor and control node instances. As a bonus you can also run non-node tasks under PM2

Disable logging in a node.js script running with forever

I am continually running a few server scripts (on different ports) with nodejs using forever.
There is a considerable amount of traffic on some of these servers. The console.log commands I have for tracking connection anomalies result in bloated log files that I don't need all of the time - only for debugging. I have been manually stopping the scripts late at night, truncating the files, and restarting them. This won't do for long term, so we decided to find a solution.
Someone else on my system deleted the log files I had set up for each of the servers without my knowledge. Calling forever list on the command line shows that the server scripts are still running but now I can't tail the log files to see how the nodes are doing.
Node downtime should be kept to a bare minimum, so I'm hesitant to stop the servers during daylight hours for longer than a few minutes. Initial testing from the client side seems to indicate that the scripts are doing fine, but I can't be 100% sure there are no errors due to failed attempts at logging to a nonexistent file.
I have a few questions actually:
Is it ok to keep forever running like this?
If not, is there a proper way to disable logging? The github repository seems to indicate that forever will still log to a default file, which I don't want. Otherwise I may just write a cronjob that periodically stops scripts, truncates logs, then restarts the scripts.
What happens if I just create the logfile again with something like touch logfile_name.log while the script is still running - will this make forever freak out or is this a plausible solution?
Thanks for your time.
according to https://github.com/foreverjs/forever, try to pass -s to silent all log.
forever -s start YOURSCRIPT
Surely, before doing this, try to update forever to the latest:
sudo curl -L https://npmjs.com/install.sh | sudo sh
sudo npm update -g.
1) Just build in a periodic function or admin option to clear the forever logs. From the manual forever cleanlogs
2) At least for linux. Send each log file to /dev/null. Each log type is specified by options -l -o and -r. The -a option for append log, will stop it complaining about the log already existing.
forever start -a -l /dev/null -o /dev/null -r /dev/null your-server.js
Perhaps employ your own logging system, I use log4js, it doesn't complain if I delete the log file while the node process is still running.
There's a nifty tool that can help you that called logrotate. Have a look here
Especially the copytruncate option, it is very useful in your case.

Resources