CI/CD services will often use SUCCEDED or FAILED to display if a deployment succeded and if an error occurred. However, with PM2 this is not so easy.
If you do:
pm2 start ecosystem.config.js in one of your deployment steps, PM2 will start the process but won't report an error if one of the processes fails. To check it, you would need to execute pm2 status, and then judging by the number of restarts and by executing pm2 logs --err you would be able to see what happens. I would like PM2 to return a non-zero code or be verbose about a process failing on first launch, rather than what it does now (failing silently).
Is there a native PM2 command that makes it integrate with CI/CD release status reporting easily?
What I want to achieve:
pm2 start ecosystem.config.js
# one of the processes in ecosystem.config.js fails, because eg. the database engine is not running and the driver can't connect to the db
# pm2 prints to stderr and a failure shows up on my CI/CD service. pm2 closes and returns non-zero if it failed to launch one of the processes
What's happening currently:
pm2 start ecosystem.config.js
# one of the processes fails
# no error is printed, everything is ok according to pm2. pm2 keeps running and the problematic process fails silently
Related
I have tried so many ways to keeping node server alive on Linux environment but nothing has worked. Sometime the server runs only 4-5 hours and sometime it runs 10-12 hours and after that server goes shut down automatically.
I have tried forever start, pm2, nodemon but nothing has worked.
I have also tried shell script with forever start for running it but that also not worked.
Applications that are running under PM2 will be restarted automatically if the application crashes or is killed, but an additional step needs to be taken to get the application to launch on system startup (boot or reboot). Luckily, PM2 provides an easy way to do this, the startup subcommand.
The startup subcommand generates and configures a startup script to launch PM2 and its managed processes on server boots:
$ pm2 startup systemd
Run the command that was generated (similar to the highlighted output above, but with your username instead of sammy) to set PM2 up to start on boot (use the command from your own output):
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy
check here for details https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
You can increase the size of memory restar- check this: pm2 process crashed on server. it gives an error
Try using process manager for making the application run all the time. Here is the link for Pm2 . It will restart you application once it crashes also automatically
Use a NPM package called nodemon
npm install -g nodemon
nodemon index.js
If the application fails or crashes for any reason it will restart
Read more at
https://www.npmjs.com/package/nodemon
in an Ubuntu Server, I am unable to run the node.js app in cluster mode using PM2.
The command I use is :
PM2 start server.js --name Server -i max
When I list the PM2 processes, I can see the Server has Error status.
I have tried looking into the log file generated by PM2 but it's empty.
I am however able to run the same server.js without the cluster mode using :
PM2 start server.js --name Server
doing PM2 Kill and starting all the services again was the solution to above issue.
You could also have used pm2 restart Server to restart it
If you use pm2 kill you will just kill all processes, to clean up afterwards i would recommend to use pm2 flush so all logfiles will be reset
I have gone through this same kind of situations but in my case pm2 is showing error status cause of error in my code.
use the below command
pm2 logs
pm2 logs command helped me by showing some hints to check where exactly the error is occured.
if everything works fine then pm2 list will show you the status online.
you can check the ports running by pm2(not only pm2 but all the process) using below command
sudo netstat -tulpn
I am running my node application on AWS EC2 server.
For continuous integration I installed Jenkins, on EC2, jenkins polls a code commit continuously and when a commit occurs executes some commands written in a script.
The last command is
pm2 start server.js
Everything works fine and the build is shown successful but later when I access the URL the site doesn't show.
I have a nginx server in front of node server, it gives
502 Bad Gateway
On checking I realised the node application is not running, so I cehcked pm2 logs and found this happening -
2016-08-12 07:53:28: [[[[ PM2/God daemon launched ]]]]
2016-08-12 07:53:28: BUS system [READY] on port /var/lib/jenkins/.pm2/pub.sock
2016-08-12 07:53:28: RPC interface [READY] on port /var/lib/jenkins/.pm2/rpc.sock
2016-08-12 07:53:28: Starting execution sequence in -fork mode- for app name:server id:0
2016-08-12 07:53:28: App name:server id:0 online
2016-08-12 07:53:28: pm2 has been killed by signal, dumping process list before exit...
2016-08-12 07:53:28: Deleting process 0
2016-08-12 07:53:28: Stopping app:server id:0
2016-08-12 07:53:28: App [server] with id [0] and pid [25822], exited with code [0] via signal [SIGTERM]
2016-08-12 07:53:28: Proc is not defined anymore or is being killed
2016-08-12 07:53:28: [PM2] Exited peacefully
pm2 is getting killed as soon as starting, don't know why, tried reinstalling pm2 from npm didn't work.
Ubuntu 14.04 LTS
Node v4.4.7 LTS
npm v2.15.8
pm2 v1.1.3
Work is halted, need help immediately, please.
The answer is in the comments given by Dusan Bajic, if anyone is doing continous integration with jenkins on EC2 using a node application and similar thing happens with pm2 just add this line in script before starting pm2.
export BUILD_ID=dontKillMePlease
and instead of
pm2 start server.js
use
pm2 restart server.js
server.js being your application or else build will fail if in jenkins if you commit and it runs script again cause pm2 is already running server.js and won't stop.
From ProcessTreeKiller
To reliably kill processes spawned by a job during a build, Jenkins
contains a bit of native code to list up such processes and kill them
...
How it works
The ProcessTreeKiller takes advantage of the fact that by
default a new process gets a copy of the environment variables of its
spawning/creating process.
It sets a specific environment variable in the process executing the
build job. Later, when the user requests to stop the build job's
process it gets a list of all processes running on the computer and
their environment variables, and looks for the environment variable
that it initially set for the build job's process.
Every job with that environment variable in its environment is then
terminated.
If your build wants to leave a daemon running behind...
A convenient way to achieve that is to change the environment variable
BUILD_ID which Jenkins's ProcessTreeKiller is looking for. This will
cause Jenkins to assume that your daemon is not spawned by the Jenkins
build. For example:
BUILD_ID=dontKillMe /usr/apache/bin/httpd
I have a node chat application that needs to keep running on my server (ubuntu with nginx). The problem is that the application stops after a few hours or days.
When I check on the server I see that my pm2 list is empty.
The code I use to start my app:
pm2 start notification_server/index.js
It somehow looks as if pm2 is reset after a while. I also tried using forever, but then I run into the same problem. Is there some way to prevent the pm2 list from getting empty?
This is most likely an indication that your server is rebooting. When your server reboots, PM2 shuts down and deletes all Node instances from its "status" list.
You can perform the following steps to make PM2 relaunch your Node programs start back up on reboot:
Run pm2 startup and follow the directions (you will have to perform a sudo command; PM will tell you exactly what to do).
Through pm2 start, get your Node processes up and running just like you like them.
Run pm2 save to register the current state of things as what you want to see on system startup.
Source: http://pm2.keymetrics.io/docs/usage/startup/
Did you try checking logs $ pm2 logs for you application?
Most likely it will tell you why your application was terminated or maybe it just exited as it supposed to. You could find something like that there:
PM2 | App [app] with id [0] and pid [11982], exited with code [1] via signal [SIGINT]
This can tell you what happened. Without more details, it's hard to give you a better answer.
I am running a forever process for Node.js server but after one day the server stops the process.My server is running on Ubuntu platform. I have done the following process:
First I installed npm install forever and ran the command forever start server.js. I need the server to run for all the time but after one day I am seeing the server stops working.
Please help me to resolve this issue.
I would like to suggest that you try PM2 instead. Here's the short tutorial I wrote about it: http://www.nikola-breznjak.com/blog/nodejs/using-pm2-to-run-your-node-js-apps-like-a-pro/.
edit:
As per StackOverflow's policy I'm including the content from the post here also:
Running your Node.js application by hand is, well, not the way we roll. Imagine restarting the app every time something happens, or god forbid application crashes in the middle of the night and you find about it only in the morning – ah the horror. PM2 solves this by:
allowing you to keep applications alive forever
reloading applications without downtime
facilitating common system admin tasks
To install PM2, run the following command:
sudo npm install pm2 -g
To start your process with PM2, run the following command (once in the root of your application):
pm2 start server.js
As you can see from the output shown on the image below, PM2 automatically assigns an App name (based on the filename, without the .js extension) and a PM2 id. PM2 also maintains other information, such as the PID of the process, its current status, and memory usage.
As I mentioned before, the application running under PM2 will be restarted automatically if the application crashes or is killed, but an additional step needs to be taken to get the application to launch on system startup (boot or reboot). The command to do that is the following:
pm2 startup ubuntu
The output of this command will instruct you to execute an additional command which will enable the actual startup on boot or reboot. In my case the note for the additional command was:
sudo env PATH=$PATH:/usr/local/bin pm2 startup ubuntu -u nikola