Disable pm2 log creation in node js - node.js

I am using aws ubuntu instance for my application and using node js to sync data from third party apis.
I have 6 node js scripts that calls every minute to sync data by making http request for many users.
For process management I am using PM2 module of node js.This pm2 creates logs for each running file ,this files size increasing tremendously and instance space occupied by log files.
Is there any way to disable pm2 log creation?

We can user pm2 application declaration for our purpose and we can use:
Disabling logs
You can pass /dev/null to error_file or out_file to disable logs saving.
We can also set max_memory_restart parameter in process.json apps to set autorestart process on certain memory occupied by memory.example below for process.json:
{
"apps" : [{
"script": "worker.js",
"watch": true,
"max_memory_restart": "50M",
"error_file": "/dev/null"
}]
}

Related

Run nextjs application in the background

I have an application built with nextJs and this application should work on a local server (Windows).
my customer told me that he needed this application to work in the background after searching I found that I needed to use a package called pm2 and when I used it gives me an error and I found that I needed to make some configurations for it and I can't found any helping resources, please help 💔
I found that to run nextJs application in the background you will need a custom configuration
you need to download the pm2 globally in your system
create a file with the name ecosystem.config.js in the root folder next to the package.json file
you need to put your config data in this file which would be something like this
module.exports = {
apps: [
{
name: "inventory_test",
script: "node_modules/next/dist/bin/next",
args: "start -p 3333", //running on port 3000
watch: false,
},
],
};
you should set the name as the name you want to see when you check
the list of pm2
the problem will be solved when you set the script as I did in the code above to be more precise the default run of pm2 is to go to the node js folder in the system and try to make start for the application using npm directly but this is the problem we need to make it use the node runner from the nextjs itself or something like this so we change the script as above
after that, we set the arguments that we should run after the npm and in my example is the arg start and choose the port for our application too
and now we make our config
NOTES
you should make build before you start the application
to run the project you will open the folder of the project in the terminal || cmd || cmder and run the command pm2 start ecosystem.config.js

Get node console output to file with pm2

How can I get node console output (such as errors) to a file with pm2?
I tried to run pm2 task --log-type json but it didn't work
There is no log file in /var/log or in the project directory.
How can I achieve this?
Just add log property to your ecosystem config file like this:
module.exports = {
apps: [
{
name: name,
script: './server/server.js',
error_file: './logs/err.log',
out_file: './logs/out.log',
log_date_format: 'YYYY-MM-DD HH:mm:ss:SSS',
}
]
}
Then pm2 will automate write log into these file (one for console.log and one for console.error)
Use pm2 list and note process-id for which you want to see the log.
Save the executed process using pm2 save
Then, enter pm2 logs process-id to see the logs and report of respective pm2 process.

Running forever script from Google Cloud Platform App Engine startup script

I have edited the startup-script variable for one of my instances running on the Google Cloud Platform App Engine. I'd like it to call a forever script to make sure my node app is running. So I added:
cd /opt/bitnami/apps/myapp
forever start --workingDir /opt/bitnami/apps/myapp/ --sourceDir /opt
/bitnami/apps/myapp/ app.js
after the #!/bin/bash line (also tried without the cd as it's not really necessary based on my command). But once the vm is started, running a forever list doesn't list my forever task as having ever started. If I copy and paste that forever command into a gcloud terminal and run, the task shows up fine and my app starts no problem.
Am I not calling this correctly somehow within the bash script?
The simple answer is that GAE does this by default. No need for forever or PM2. There are certain health checks that GAE does on the Docker container holding your app, and if they do not pass the instance is automatically restarted
If you want granular control over these checks (called Legacy Health Checks) you can add this to your app.yaml file:
health_check:
enable_health_check: True
check_interval_sec: 5
timeout_sec: 4
unhealthy_threshold: 2
healthy_threshold: 2
There is also updated mechanisms (called Updated Health Checks) that are still in beta, but can be used instead
The proper way to start your nodejs app on appengine is to specify the "scripts" field in your package.json, as the documentation
Below is an example borrowed from this sample
"scripts": {
"start": "node ./bin/www",
"test": "cd ..; npm run t -- appengine/analytics/test/*.test.js"
},
If you however, are only interested in running a node script, and not interested in the features that come with Google app engine, then you may simply run it on a Google Compute Engine instance.

Redeploying NAR (node.js) archive with PM2

There is node.js app built with Typescript, so it needs to be first "compiled" to JS before it gets run. I'm planning to use NAR (https://github.com/h2non/nar) to build ready-to-deploy package to avoid fiddling with npm install and compiling it on production. I also use PM2 as process manager for node apps.
However as far as I know PM2 can only deploy from git (fetching sources and calling npm install etc. later on), but I couldnt find a way to easily deploy application that is already pre-built.
This is my deploy.yml file contained within archive that I extract with nar extract <package>:
apps:
- script: dist/app.js
merge_logs: true
name: server
instances: 1 # 0 => max, depending on CPU cores
exec_mode: cluster
node_args: --harmony --harmony_destructuring --harmony_default_parameters
log_file: deploy/logs/server.log
pid_file: deploy/pids/server.pid
source_map_support: true
env:
NODE_ENV: production
It works fine when run for the first time, but then when I try to redeploy it (replacing application content with new version) and call pm2 reload all I get errored processes saying they either cannot load ProcessManager from PM2 or cannot find my .env file (which is in place).
As soon as I kill PM2 daemon with pm2 kill and start apps again with pm2 start all deploy.yml it clicks. But this is probably not how PM2 should be used, right?
Do you have any experience with such setup and had similar issues? Or maybe can you point me to another way of running my deployment?

How to start node.js app with pm2 in cluster mode?

We are trying to start our app with pm2 0.12.8 on ubuntu 14.04 with octa core proccessor. The read me on the git hub has a very straight forward command for running node app in cluster mode.
# Cluster mode
$ pm2 start app.js -i 0 **# Will start maximum processes with LB depending on available CPUs**
$ pm2 start app.js -i max **# Same as above, but deprecated yet.**
But the above command are not working for us. When we try to run these commands only one instance is listed by pm2.
Why?
Any suggestion
Thanks
have you tried starting a fixed number of processes? i.e.
pm2 start app.js -i 2 //should start two instances.
what does "pm2 monit" show you?
also try
pm2 stop all
pm2 delete all
and then
pm2 start app.js -i 0
if you stop a process in pm2 it still reserves one cpu for it even if its not running. you should allways use pm2 delete
Since you are looking to use a process file to manage your pm2, the process file should look similar to this:
// ecosystem.js
{
"apps" : [{
"name" : "API",
"script" : "server.js",// name of the startup file
"instances" : 4, // number of workers you want to run
"exec_mode" : "cluster", // to turn on cluster mode; defaults to 'fork' mode
"env": {
"PORT" : "9090" // the port on which the app should listen
}
// for more options refer : http://pm2.keymetrics.io/docs/usage/application-declaration/#process-file
}]
}
Run this app using the following command to start and stop respectively:
$ pm2 start ecosystem.js
$ pm2 stop ecosystem.js
For fresh process
pm2 start app.js --name "my-node-app" -i 2 // to create 2 process
To make existing running process. You have to stop and delete the current running process, if it was fork mode. Then only it can create cluster mode.
pm2 stop my-node-app
pm2 delete my-node-app
pm2 start app.js --name "my-node-app" -i 2 // to create 2 process
I think you might ever start this project with normal mode (fork_mode), so you should delete all process list before change to cluster mode, since pm2 will memorise ur start options
pm2 delete all
pm2 start app.js -i [NUMBER_OF_INSTANCE|max]
You can get the best information here : pm2 cluster mode
To enable the cluster mode, just pass the -i option:
pm2 start app.js -i max
max means that PM2 will auto detect the number of available CPUs and run as many processes as possible
Or via a js/yaml/json file:
module.exports = {
apps : [{
script : "api.js",
instances : "max",
exec_mode : "cluster"
}]
}
NOTE: you need to set the exec_mode to cluster so PM2 know you want to load balance between each instances, by default it will not
Then to start the Process File:
pm2 start processes.json
The -i or instances option can be:
0/max to spread the app across all CPUs
-1 to spread the app across all CPUs - 1
number to spread the app across number CPUs.

Resources