Skip to content

Instantly share code, notes, and snippets.

@ganthore
Last active February 4, 2022 17:03
Show Gist options
  • Save ganthore/b302dbf4da731eaed3cd to your computer and use it in GitHub Desktop.
Save ganthore/b302dbf4da731eaed3cd to your computer and use it in GitHub Desktop.
See README below.
INSTALLATION
Copy jenkins-slave.init to /etc/init.d/jenkins-slave and copy jenkins-slave to /etc/sysconfig/jenkins-slave.
cp jenkins-slave.init /etc/init.d/jenkins-slave
cp jenkins-slave /etc/sysconfig/jenkins-slave
chkconfig jenkins-slave on
service jenkins-slave start
Change the values of /etc/sysconfig/jenkins-slave to match your environment needs.
It's critical that JENKINS_URL and JENKINS_NODENAME are set.
The node name variable must match a node that you've already defined in your Jenkins nodes.
JENKINS_WORKDIR="/opt/jenkins-slave"
JENKINS_USER="jenkins-slave"
JENKINS_URL="http://127.0.0.1:8080"
JENKINS_NODENAME="slave_name"
#!/bin/bash
# chkconfig: - 85 15
# description: Init script for jenkins-slave
# Source function library.
. /etc/rc.d/init.d/functions
[ -f /etc/sysconfig/jenkins-slave ] && . /etc/sysconfig/jenkins-slave
[ -n "${JENKINS_URL}" ] || exit 0
[ -n "${JENKINS_WORKDIR}" ] || exit 0
[ -n "${JENKINS_USER}" ] || exit 0
[ -n "${JENKINS_NODENAME}" ] || exit 0
[ -x /usr/bin/java ] || exit 0
create_user()
{
awk -F":" '{ print $1 }' /etc/passwd | grep -x ${JENKINS_USER} > /dev/null
if [ $? -eq 1 ]; then
useradd ${JENKINS_USER} -d ${JENKINS_WORKDIR} -s /bin/false
fi
}
create_dir()
{
if [ ! -d ${JENKINS_WORKDIR} ];
then
mkdir -p ${JENKINS_WORKDIR}
fi
}
create_logdir()
{
if [ ! -d /var/log/jenkins-slave ];
then
mkdir -p /var/log/jenkins-slave
chown -R ${JENKINS_USER}:${JENKINS_USER} /var/log/jenkins-slave
fi
}
download_jar()
{
curl -s -o ${JENKINS_WORKDIR}/slave.jar ${JENKINS_URL}/jnlpJars/slave.jar || exit 0
chown -R ${JENKINS_USER}:${JENKINS_USER} ${JENKINS_WORKDIR}
}
start()
{
create_user
create_dir
create_logdir
cd ${JENKINS_WORKDIR}
[ -f slave.jar ] || download_jar
echo -e $"Starting Jenkins BuildSlave..."
su - ${JENKINS_USER} sh -s /bin/bash -c "\
/usr/bin/java -jar ${JENKINS_WORKDIR}/slave.jar \
-jnlpUrl ${JENKINS_URL}/computer/${JENKINS_NODENAME}/slave-agent.jnlp \
>/var/log/jenkins-slave/jenkins-slave.log 2>&1 &"
}
stop()
{
echo -e $"Shutting down Jenkins BuildSlave..."
pkill -f 'java.*slave.jar'
}
# 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
@DBarthe
Copy link

DBarthe commented Sep 26, 2017

Thanks :)

@vazhnov
Copy link

vazhnov commented Jan 22, 2021

Hello @ganthore !

I found link to this code from https://support.cloudbees.com/hc/en-us/articles/207849467-CentOS-JNLP-startup-script

Could you please add information about license of this repository — is it allowed to copy, change and use the code and board schematic files in any purpose?
It would be great to see any popular open source license.

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