Last active
December 8, 2023 23:40
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Hi,
How can get the jenkins api as mentioned in curl url.
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
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.