Kevin Bridges
Kevin Bridges

Reputation: 186

I would like to find the process id of a Jenkins job

I would like to find a way to find the process id of a Jenkins job, so I can kill the process if the job gets hung. The Jenkins instance is on Ubuntu. Sometimes, we are unable to stop a job via the Jenkins interface. I am able to stop a job by killing the process id if I run a Jenkins job that contains a simple shell script where I manually collect the process id such as:

#!/bin/bash
echo "Process ID: $$"
for i in {1..10000}
do
   sleep 10;
   echo "Welcome $i times"
done

In the command shell, I can run sudo kill -9 [process id]and it successfully kills the job.

The problem is, most of our jobs have multiple build steps and we have multiple projects running on this server. Many of our build steps are shell scripts, windows batch files, and a few of them are ant scripts. I'm wondering how to find the process id of the Jenkins job which is the parent process of all of the build steps. As of now, I have to wait until all other builds have completed and restart the server. Thanks for any help!

Upvotes: 2

Views: 5827

Answers (2)

Kevin Bridges
Kevin Bridges

Reputation: 186

This is an update to my question. For killing hung (zombie) jobs, I believe that this will only work for cases where Jenkins is running from the same server as its jobs. I doubt this would work if you are trying to kill a hung process running on a Jenkins slave.

#FIND THE PROCESS ID BASED ON JENKINS JOB
user@ubuntu01x64:~$ sudo egrep -l -i 'BUILD_TAG=jenkins-Wait_Job-11' /proc/*/environ
/proc/5222/environ
/proc/6173/environ
/proc/self/environ


# ONE OF THE PROCESSES LISTED FROM THE EGREP OUTPUT IS THE 'EGREP'COMMAND ITSELF, 
# ENSURE THAT (LOOP THROUGH) THE PROCESS ID'S TO DETERMINE WHICH IS
# STILL RUNNING
user@ubuntu01x64:~$ if [[ -e /proc/6173 ]]; then echo "yes"; fi 
user@ubuntu01x64:~$ if [[ -e /proc/5222 ]]; then echo "yes"; fi 
yes

# KILL THE PROCESS
sudo kill -9 5222

Upvotes: 2

luka5z
luka5z

Reputation: 7805

On *nix OS you can review environment variables of a running process by investigating a /proc/$pid/environ and look for Jenkins specific variables like BUILD_ID, BUILD_URL, etc.

cat /proc/'$pid'/environ | grep BUILD_URL

You can do it know you $pid or go through of running processes.

Upvotes: 4

Related Questions