Skip to content

Instantly share code, notes, and snippets.

@hyoshida
Last active August 29, 2015 14:25
Show Gist options
  • Save hyoshida/0b806fc6870c90ea0a38 to your computer and use it in GitHub Desktop.
Save hyoshida/0b806fc6870c90ea0a38 to your computer and use it in GitHub Desktop.
Restart Jenkins from the build of Jenkins
#!/bin/sh -e
CWD=$WORKSPACE/..
echo "cd $CWD"
cd $CWD
if [ `ruby queue_count.rb` -gt 0 ]; then
echo "Too busy..."
exit 1
fi
echo "Restarting jenkins..."
# refs: https://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build
daemonize -E BUILD_ID=${BUILD_ID} $CWD/restart-jenkins
echo "SUCCESS!"
suid: restart_jenkins
sudo chown root restart_jenkins
sudo chmod u+s restart_jenkins
restart_jenkins: restart_jenkins.c
cc restart_jenkins.c -o restart_jenkins
#!/usr/bin/env ruby
require 'open-uri'
require 'json'
json = open('http://localhost/queue/api/json').read
hash = JSON.parse(json)
puts hash['items'].count
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
int main() {
if (setuid(0) == -1) {
printf("setuid: %s\n", strerror(errno));
return 1;
}
return system("service jenkins restart");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment