Hello Stackoverflow Coders, I have 3 node js script with 3 different port numbers on my web hosting account.
Am using PM2 to start the node js script.
I have succeded in starting two script using pm2
For example
pm2 start script1.js
pm2 start script2.js
the script1.js and script2. js were assigned id 0 and 1 respectively.
but when i tried to start sccript 3, it says
[PM2] Starting /server/script3.js in fork_mode (1 instance)
[PM2] Done.
please what could be the issue. how do i start the third script since its port number is different...
Thanks
Related
When I start pm2, I get this
Error: ENOENT: no such file or directory, open 'C:\WINDOWS\system32\.pm2\module_conf.json'
how can I fix this?
1 . Add user environment variable as below:
PM2_HOME=%USERPROFILE%\.pm2
2 . Restart command line
cmd
3 . Then kill pm2 daemon once
pm2 kill
4 . Now run pm2 as usual (An example is below)
pm2 start ./bin/www
Previously I have had success implementing PM2, but not currently.
My node app does run just fine if I start it manually, but nothing I do gets it to run via PM2, even though PM2 appears to be starting it up. Here's what I mean:
If I run pm2 start server/index.js, the response in the terminal reads:
$ pm2 start server/index.js
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting D:\Program Files\nodeApps\service-management-api\server\index.js in fork_mode (1 instance)
[PM2] Done.*
Then the terminal prints out a table with App info. It doesn't look pretty pasted here so I'll list it out:
App Name: index
id: 0
version: 1.0.0
mode: fork
pid: 8984
status: online
restart: 0
update 0s
cpu: 0%
mem: 26.0 MB
user: ME
watching: disabled
It appears that the node process should be running. But if I immediately enter pm2 list it shows no processes running. If I enter pm2 stop index, it says:
$ pm2 stop index
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2][ERROR] Process index not found
Alternatively, if I try using ecosystem.config.js in the commands, I get similar results. Here are the commands tried:
pm2 reload ecosystem.config.js
pm2 start ecosystem.config.js
Example result of running those commmands:
$ pm2 start ecosystem.config.js
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2][WARN] Applications sm_api not running, starting...
[PM2] App [sm_api] launched (2 instances)
And CLI prints table showing two instances with status "online" and watching "enabled". And yet, app isn't running (when tested from browser) and "pm2 show " returns:
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2][WARN] <app name> doesn't exist
Any clues what's gone awry with my pm2?
Heres my ecosystem.config.js file:
module.exports = {
apps : [{
name: 'sm_api',
script: 'server/index.js',
"log_date_format" : "YYYY-MM-DD HH:mm Z",
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'one two',
instances: 'max',
error_file : "C:\\pm2_system\\.pm2\\logs\\sm-api-error",
out_file: "C:\\pm2_system\\.pm2\\logs\\sm-api-out",
autorestart: true,
watch: "../",
max_restarts: 10,
max_memory_restart: '1G',
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
},
exec_mode: 'cluster'
}],
};
Running in Windows Server 2012 environment. Note that I intend to add pm2-windows-service package after I get pm2 working.
Just a note for those who are saying to run this in no-daemon mode, (pm2 start --no-daemon), you should use this mode if you're trying to diagnose the reason why when you run with the daemon, your script fails.
Almost invariably, this is because you're telling PM2 to watch a dist folder or similar while it's building, so PM2 keeps restarting until it hits its limit (because files are being regenerated at pretty rapid speed) and then outputs the "errored" status.
Running in no-daemon is absolutely not recommended in production. Ensure that PM2 is running as a daemon so it can itself restart, and so it can restart your processes as a process itself. If you kill your terminal sessions or they are automatically timed out on your host, you will quickly find your service dies when that happens.
So, in short.. do it the right way and figure out what the problem is, rather than being lazy and turning PM2 into a glorified wrapper for the node binary.
Ok, I got the answer after posting an issue to the pm2 github issues page.
Sharing it here in case anyone else finds themselves in this situation:
https://github.com/Unitech/pm2/issues/4113
(Basically pm2 3.2.5 introduced a bug that causes this issue in Windows. My dev install was 3.2.4. The issue was resolved by reverting to 3.2.4 in production. Simple process, see instructions at link above.)
As a workaround I used the following:
pm2 start --no-daemon app.js
In my case (pm2 v3.2.2):
pm2 stop all //stop all
rm ./pm2/*.pid //delete all
pm2 start app.config.js
I am trying to have a nodejs application start automatically on system boot. Basically all I need is to run the command node /dir/app.
I am using openwrt on an Arduino Yun. And have tried a couple things.
On the openwrt website it said I can do this. https://wiki.openwrt.org/inbox/procd-init-scripts :
#!/bin/sh /etc/rc.common
USE_PROCD=1
start_service() {
procd_open_instance
procd_set_param command node ///www/www-blink.js
procd_close_instance
}
I have also tried changing the dir to /www/www-blink.js not ///
However i'm not sure what i'm doing wrong as nothing comes up when I try run it with /etc/init.d/node-app start I am obviously writing the code wrong but i'm not sure what it should exactly look like.
The other thing I have tried is the node modules forever and forever-service.
I downloaded them on my computer using npm install -g forever and forever-service aswell. I transfered them to usr/lib/node_modules on my arduino yun. However when I try to use and forever(-service) commands it says
-ash: forever: not found
I have tried a couple other things, however nothing has worked. Any help would be greatly appreciated.
-- I also need to be able to start my express script with npm start not node app but I guess the first thing is getting it to work at all.
you can put the starting command (node /dir/app &)in the /etc/rc.local script. This will start your nodejs application automatically on system boot.
OpenWRT procd has a "respawn" parameter, which will restart a service that exits or crashes.
# respawn automatically if something died, be careful if you have an
# alternative process supervisor if process dies sooner than respawn_threshold,
# it is considered crashed and after 5 retries the service is stopped
procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
So, you cold just add:
procd_set_param respawn 60 5 5
or something like that to your OpenWRT procd initscript. This 60 5 5 means it will wait 5s between respawns (middle parameter), and if it respanws more than 5 times (last parameter) in 60s (first parameter), it will disable the service ("restart loop" detected).
Refer to this page for more information:
https://openwrt.org/docs/guide-developer/procd-init-scripts
You need to execute your node application like a Linux Service.
Upstart is perfect for this task
Upstart is an event-based replacement for the /sbin/init daemon which handles starting of tasks and services during boot, stopping them during shutdown and supervising them while the system is running.
If you have an app like this (for example):
// app.js
var express = require('express')
var app = express()
var port = process.env.PORT
app.get('/', function(req, res) {
res.send('Hello world!')
})
app.listen(port)
With a package.json like this:
{
"name": "my-awesome-app",
"version": "1.0.0",
"dependencies": {
"express": "^4.13.3"
},
"scripts": {
"start": "node app.js"
}
}
We create a upstart configuration file called myAwesomeApp.conf with the following code:
start on runlevel [2345]
stop on runlevel [!2345]
respawn
respawn limit 10 5
setuid ubuntu
chdir /opt/myAwesomeApp.conf
env PORT=3000
exec npm start
To finish, put your application (app.js and package.json) in the /opt/myAwesomeApp.conf and copy the configuration file myAwesomeApp.conf in /etc/init/
This is all, now you just need to run service myAwesomeApp start to run your node application as a service
I've never used procd before, but it likely needs the full path to node (e.g., /usr/bin/node). You'd need to make the line something like procd_set_param command /usr/bin/node /www/www-blink.js, assuming the file you want to run is /www/www-blink.js. You can locate node by running which node or type -a node.
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.
Is there some way to configure multiple worker and/or web processes to run in the single Heroku app container? Or does this have to be broken up into multiple Heroku apps?
For example:
worker: node capture.js
worker: node process.js
worker: node purge.js
web: node api.js
web: node web.js
All processes must have unique names. Additionally, the names web and worker are insignificant and carry no special meaning. The only process that carries a significant name is the web process, as stated in the Heroku docs:
The web process type is special as it’s the only process type that
will receive HTTP traffic from Heroku’s routers. Other process types
can be named arbitrarily. -- (https://devcenter.heroku.com/articles/procfile)
So you want a Procfile like so:
capture: node capture.js
process: node process.js
purge: node purge.js
api: node api.js
web: node web.js
You can then scale each process separately:
$ heroku ps:scale purge=4