i'm running my node.js app on the linux server using PM2, with a config file, like this:
PM2 start mywebsite.config.js
all is good. but now i want to add jenkins to the picture.
i'm running a pipeline project in jenkins, using Jenkinsfile.
All working fine except for the last command, that should restart the app, to make the new version live:
stage('Restart PM2') {
steps {
sh 'pm2 restart all' }
}
}
and this command fails. here is the log output:
+ pm2 restart all
Use --update-env to update environment variables
[PM2][WARN] No process found
< empty pm2 log table here>
Use `pm2 show <id|name>` to get more details about an app
I understand that PM2 is working per user. means, that the user who ran the first command (start) is the one that should run the restart as well.
but how to do this?
To run pm2 restart all from Jenkins you need to:
Configure your system to run sudo from jenkins
(https://sgoyal.net/2016/11/18/run-a-shell-from-jenkins-using-sudo-ubuntu/)
Make a symbolic link to the .pm2/ folder, in my case(Ubuntu) it was at /root/.pm2 so i run
sudo ln -s /root/.pm2/ /var/lib/jenkins/
NOTE: /var/lib/jenkins if the default jenkins root directory, you can check yours on Jenkins configuration
after that you can go to jenkins and setup a shell command, in my case i did:
#!/bin/sh
echo "RESTARTING ALL"
sudo pm2 restart all
echo "ALL RESTARTED"
NOTE: if you have a .pm2 folder already in your jenkins root directory rename it so you can do the symbolic link
Hope this helps
Instead of restarting PM2 through you jenkins code, let PM2 do it by itself, using the watch flag. in your config file, set watch to be true.
You may want to add a relatively new flag called watch-ignore. that's an array, with files to be ignored by the watch. add your log file and error file to this list. otherwise, any logged information will cause your node app to restart endlessly.
after doing these changes to the config file, run pm2 again with the config. remove the restarting code from Jenkinsfile, you don't need that anymore, pm2 will detect the new version and will reload the app!
BUILD_ID=dontKillMe PM2 start mywebsite.config.js
Jenkins kills the pm2 daemon to be created by the build.
You should put the keyword to prevent killing daemon by Jenkins.
Related
I want to set up ApplicationStart hook for node.js project where pm2 is used as a process manager in aws ec2 server.
I checked some tutorials and the shell script for ApplicationStart hook contains commands for running the project by using:
node/npm
pm2
for example in this tutorial, the shell script contains:
npm start
pm2 start npm --name "covidapp" -- start
in this tutorial, the shell script contains:
pm2 start npm --name "myApp"
node app.js > app.out.log 2> app.err.log < /dev/null &
Why we are running the project two times? Why we just don't use pm2?
I've deployed several apps on EC2 using PM2 and in my experience there should be no need (or benefit) to use node app.js, npm start or similar.
As you are probably already guessing, the whole point of PM2 is to run the process(es).
My recommendation would be to create a PM2 ecosystem configuration with all needed configurations, number of processes, ENV vars etc. I personally prefer this way even when running only one single node application on the server.
https://pm2.keymetrics.io/docs/usage/application-declaration/
And start the process(es) using the configuration, eg.:
pm2 start ecosystem.config.js
I also recommend using PM2 startup generator to make sure PM2 is started on server reboot: pm2 startup
https://pm2.keymetrics.io/docs/usage/startup/
Once you have the startup script generated. Start your processes using pm2 manually or by using a configuration file (see example above). Verify with pm2 status that all processes are running as expected and execute pm2 save to "snapshot" the current state. The saved state will now automatically respawn on reboot.
I am using pm2 for managing node processes on one of the servers.
The package is here: https://pm2.keymetrics.io/
It is open source and available both on npmjs and GitHub.
I can easily install it every time using: npm i pm2 -g
I love pm2, and not just node processes, I write bash scripts and run them as cron under pm2 and I can easily check the logs.
Some commands:
pm2 --name "process-name" start "bash script.sh"
pm2 --name "node-process" start "node main.js"
pm2 logs node-process
pm2 stop node-process
pm2 restart node-process
There are 2 more commands which are very useful to start pm2 on startup with all the processes automatically.
pm2 startup Will generate startup script.
pm2 save Will update start script with current processes.
Everything is good. But, today I got into a problem.
I am running all pm2 node processes from a folder /mnt/node.
What I want is that I have synced that /mnt/node folder to another server and I am trying to find a way to move all pm2 processes automatically to another server without writing each process once again.
May be someone can help.
You can do this.
On the source server:
pm2 save
copy file saved on ~/.pm2/dump.pm2 to destination server, then:
pm2 resurrect
Haven't try this between two differents server yet but i think it will be ok.
I am working with Jenkins and I want to start a script with forever using Jenkins with a different user named aroot.
So my build configuration I write this command:
sudo -u aroot forever start -a --uid 'server' bin/www
It works fine but forever still tries to access the jenkins user and when I try to see the scripts running under forever by using the command:
forever list
I see nothing. Why does this happen? I have changed the user to aroot but it still tries to start the script under jenkins user. What should I do here?
I even tried changing the default jenkins user to aroot but after this, the Jenkins just does not restart.
you should not need the forever thing going. just enable it from systemctl
# systemctl enable jenkins.service
how you get it to run as different users is by editing this file.
# vi /etc/sysconfig/jenkins
and changing this line to whatever user you want.
JENKINS_USER="aroot"
When you restart it will be running as that user but you will need to chown all files in /var/lib/jenkins so it has the correct ownership.
I am new to pm2 manager and ssh. I developed one project in node.js and hosted into pm2 and ssh.That's Working fine and give correct output
Now my question is
Same way i hosted another project into pm2 server and then i follow below procedure to run this application.
Step1: ssh and Mycredentials
step2: cd folder projectname
step3 : pm2 start www
That shows Done.
I will run url i didn't get output default it shows first project ouput\
please help me!
Start your pm2 service with name argument to differentiate with multiple services
pm2 start www --name="my-sample-app1"
pm2 start index.js --name="my-sample-app2"
In order to list out the running process, enter the following command,
pm2 list
Using this you can debug the list of running process
To know the detailed information about your running process, use the following command
pm2 show my-sample-app1
pm2 show my-sample-app2
i tried but not working.
pm2 stop appname
pm2 delete appname
After enter above commands it show process is deleted,Then i enter second project and
pm2 start www
but i got first project output
I have a simple meteor app that I'm running on an Amazon EC2 server. Everything is working great. I start it manually with my user via meteor in the project directory.
However, what I would like is for this app to
Run on boot
Be immune to hangups
I try running it via nohup meteor &, but when I try to log out of the EC2 instance, I get the "You have running jobs" message. Continuing to log out stops the app.
How can I get the app to start on startup and stay up (unless it crashes for some reason)?
Install forever and use a start script.
$ npm install -g forever
I have several scripts for managing my production environment - the start script looks something like:
#!/bin/bash
forever stopall
export MAIL_URL=...
export MONGO_URL=...
export MONGO_OPLOG_URL=...
export PORT=3000
export ROOT_URL=...
forever start /home/ubuntu/apps/myapp/bundle/main.js
exit 0
Conveniently, it will also append to a log file in ~/.forever which will show any errors encountered while running your app. You can get the location of the log file and other stats about your app with:
$ forever list
To get your app to start on startup, you'd need to do something appropriate for your flavor of linux. You can maybe just put the start script in /etc/rc.local. For ubuntu see this question.
Also note you really should be bundling your app if using it in production. See this comparison for more details on the differences.
I am using upstart on Ubuntu server which you should be able to easily install on Amazon linux.
This is roughly my /etc/init/myapp.conf:
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
respawn
respawn limit 99 5
script
export HOME="/home/deploy"
export NODE_ENV="production"
export MONGO_URL="mongodb://localhost:27017/myappdb"
export ROOT_URL=http://localhost
export MAIL_URL=smtp://localhost:25
export METEOR_SETTINGS='{"somesetting":true}'
cd /var/www/myapp/bundle/
exec sudo -u deploy PORT=3000 /usr/bin/node main.js >> /var/log/node.log 2>&1
end script
I can then manually start and stop myapp like this:
sudo start myapp
sudo stop myapp
I believe this package solves your problem: https://github.com/arunoda/meteor-up
which seems to use forever: https://github.com/nodejitsu/forever