Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  1. Is it ok to keep forever running like this?
  2. 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.
  3. 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.

like image 532
Cubis Avatar asked Jun 10 '13 21:06

Cubis


People also ask

How do I stop a node script from running?

Method 1: Using ctrl+C key: When running a program of NodeJS in the console, you can close it with ctrl+C directly from the console with changing the code shown below: Method 2: Using process. exit() Function: This function tells Node. js to end the process which is running at the same time with an exit code.

How do I cancel my forever service?

stopAll (format) Stops all forever scripts currently running.

Why do you use forever with node js?

Forever is an npm module that ensures a Node. js script continuously runs in the background on the server. It's a helpful CLI tool for the production environment because it helps manage the Node applications and their processes.

Does node JS run all the time?

NodeJS is a runtime environment on a V8 engine for executing JavaScript code with some additional functionality that allows the development of fast and scalable web applications but we can't run the Node. js application locally after closing the terminal or Application, to run the nodeJS application permanently.


3 Answers

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.

like image 111
ChrisAdmin Avatar answered Sep 22 '22 13:09

ChrisAdmin


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.

like image 20
Paul Lan Avatar answered Sep 23 '22 13:09

Paul Lan


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.

like image 22
spotirca Avatar answered Sep 21 '22 13:09

spotirca