Last Updated: February 25, 2016
·
462
· greggles

Find jenkins jobs that were stopped in a system reboot

When you need to restart a server running Jenkins it's pretty simple: prepare Jenkins for a shutdown, wait for running jobs to finish, and then restart it.

But, if the server is rebooted for an emergency then you may need to restart some Jenkins jobs and find yourself wondering what was running.

Luckily, the jenkins "jobs" directories can help you figure out which jobs were started and didn't finish cleanly. If you have a job my_example_job then there will be a folder at $JENKINS_HOME/jobs/my_example_job/ that contains 1 directory per build for as many builds as you keep. When a build starts, it creates a directory $BUILD_ID which contains a few files. Then the build finishes it creates a few more files. And, that $BUILD_ID directory gets symlinked from a few handy directories to make it easier to refer to the build, such as a date stamp directory in the format YYYY-MM-DD_HH-MM-SS.

So, to find build directories inside the $JENKINS_HOME/jobs/ directory on November twenty-something of 2015 that have a log and do not have a build.xml:

cd /var/lib/jenkins/jobs/
find */builds/2015-11-2*/ -iname 'log' -printf '%h\n' | sort -u > ~/haslog.txt
find */builds/2015-11-2*/ -iname 'build.xml' -printf '%h\n' | sort -u > ~/hasbuild.xml.txt
grep -v -f ~/hasbuild.xml.txt ~/haslog.txt