Reputation: 731
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.
Upvotes: 3
Views: 5977
Reputation: 125
In the newer version of Jenkins, the BUILD_ID is deprecated. Instead use JENKINS_NODE_COOKIE=dontKillMe
In my Jenkinsfiles, I have used it like below
stage('Run') {
steps {
script {
sh 'export JENKINS_NODE_COOKIE=dontKillMe; pm2 --name SampleApp start npm -- start'
}
}
}
If you are invoking a shell script, then add JENKINS_NODE_COOKIE=dontKillMe in the shell script file.
Upvotes: 0
Reputation: 10879
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
Upvotes: 1
Reputation: 731
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.
Upvotes: 2