Skip to content

Instantly share code, notes, and snippets.

@jacksoncage
Last active December 8, 2023 23:40
Show Gist options
  • Save jacksoncage/5974695 to your computer and use it in GitHub Desktop.
Save jacksoncage/5974695 to your computer and use it in GitHub Desktop.
Bash script to check if a Jenkins slave node is offline and will restart node java process.
#!/bin/sh
#
# jenkins-slave: Launch a Jenkins BuildSlave instance on this node
#
# chkconfig: - 99 01
# description: Enable this node to fulfill build jobs
#
JENKINS_WORKDIR="/var/jenkins"
JENKINS_USER="jenkins"
JENKINS_URL="http://www.domain.com/"
JENKINS_NODE_LINUX=`hostname -s | cut -d'0' -f1`
JENKINS_NODE_NR=`hostname -s | cut -d'0' -f2`
JENKINS_NODENAME="${JENKINS_NODE_LINUX}_slave_node_${JENKINS_NODE_NR}"
[ -x /usr/bin/java ] || exit 0
download_jar()
{
cd $JENKINS_WORKDIR
curl -s -o slave.jar $JENKINS_URL/jnlpJars/slave.jar || exit 0
}
start()
{
cd $JENKINS_WORKDIR
[ -f slave.jar ] || download_jar
echo -n "Starting Jenkins BuildSlave ($JENKINS_NODENAME): "
sh -c "\
java -jar $JENKINS_WORKDIR/slave.jar \
-jnlpUrl $JENKINS_URL/computer/$JENKINS_NODENAME/slave-agent.jnlp \
>$JENKINS_WORKDIR/slave.log 2>&1 &"
echo Done.
}
stop()
{
echo -n "Shutting down Jenkins BuildSlave ($JENKINS_NODENAME): "
killall -9 java
killall -9 java
echo "deleting java client file "
cd $JENKINS_WORKDIR
rm -f slave.jar
echo Done.
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
;;
status)
status java
;;
*)
echo $"Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
#!/bin/bash
#
# Checking Jenkins offline and restart if offline
#
JENKINS_URL="http://www.domain.com/"
JENKINS_NODE_LINUX=`hostname -s | cut -d'0' -f1`
JENKINS_NODE_NR=`hostname -s | cut -d'0' -f2`
JENKINS_NODENAME="${JENKINS_NODE_LINUX}_slave_node_${JENKINS_NODE_NR}"
CHECKED=`curl --silent $JENKINS_URL/computer/$JENKINS_NODENAME/api/json | grep -Po '"offline":true' | cut -d':' -f2`
if [[ "$CHECKED" == "true" ]]; then
service jenkins-slave restart
echo "INFO: $JENKINS_NODENAME was offline and are now restarted"
exit 0
fi
echo "INFO: $JENKINS_NODENAME is online, no need to restart."
exit 0
@vkotovv
Copy link

vkotovv commented Mar 13, 2017

You should remove ending slash from example domain here:
JENKINS_URL="http://www.domain.com/"
because later it gets appended with another slash:
curl -s -o slave.jar $JENKINS_URL/jnlpJars/slave.jar

@devisr
Copy link

devisr commented Dec 20, 2017

Hi folks,

Can I get script customized for my issue; disconnecting a windows slave connected through JNLP & reconnecting it again.

Thanks & Regards,

Sridevi

@devisr
Copy link

devisr commented Jan 12, 2018

Hi everyone,

This script will not help for windows slave connected through JNLP I think because if the slave is offline, how can the job be run on the slave. If anyone has any suggestion for this please help me. I want the script to be run on windows slave (disconnecting & reconnecting a windows slave connected through JNLP using Jenkins job).

Thanks & Regards,

@priyankapanda348
Copy link

can i get this script customized for disconnecting a slave permanently??

Regards,
Priyanka

@mayqueen91
Copy link

If you experience issues with constantly connecting & disconnecting node, this is due to java version. Check which java your jenkins use - the node has to use the same version of java for running slave.jar.

@Azmathullahkhan786
Copy link

Hi,
How can get the jenkins api as mentioned in curl url.

@KangyiZengVizio
Copy link

I matched the java version, problem of slave.jar disconnection still exists. happens a log in packer build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment