Skip to content

Instantly share code, notes, and snippets.

@christ66
Forked from ganthore/README
Last active March 10, 2023 14:05
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save christ66/fae63fefd2b8237300cd2cac0e0dfe43 to your computer and use it in GitHub Desktop.
Save christ66/fae63fefd2b8237300cd2cac0e0dfe43 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"
JENKINS_SECRET="SECRET"
#!/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
[ -n "${JENKINS_SECRET}" ] || 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 \
-secret ${JENKINS_SECRET} \
>/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
@mastermind3043
Copy link

mastermind3043 commented May 13, 2021

Grant execute permission to the init script
sudo chmod +x /etc/init.d/jenkins-slave

@Schwaller
Copy link

Schwaller commented Sep 22, 2021

Should it not be:
sudo chmod +x /etc/init.d/jenkins-slave/jenkins-slave.init
?

@boussaffawalid
Copy link

this needed for service to auto start
sudo systemctl enable jenkins-slave.service

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