Add Jenkins Slave Java command to run at startup at Ubuntu 14.04 instance of Amazon EC2

While working with AWS Ubuntu 14.04 instance for setting up my VM for a Jenkins job I got stuck with this simple task – adding the Jenkins slave connection java command to Ubuntu startup service. I looked at different blogs and sites for the solution, even took help from developers, but unfortunately it didn’t work. So I give a break to refresh myself!

After the break (that is next day) I sat with the solutions I got from all the sources. The one I got from my colleague (who is a developer) seems good enough to serve my requirement, but I had a gut feelings that I am missing some simple thing for which it is not working.  Before going there let me start from creating the service file in the machine.

NOTE: You can scroll way down for the solution quickly 🙂

Creating Jenkins Slave script:

1. Login to the VM
2. Switch to root user
ubuntu@ip-***-**-**-**:~$ sudo -s
After switched to root user it should look like this:
root@ip-***-**-**-**:~#
3. Goto init.d directory
root@ip-***-**-**-**:~# cd /etc/init.d
After this command it will look like this:
root@ip-***-**-**-**:/etc/init.d#
4. Now create the file. We named the file as jenkins-slave
root@ip-***-**-**-**:/etc/init.d# nano jenkins-slave
Nano editor was opened and we wrote (pasted) the following in the editor.

#! /bin/sh
### BEGIN INIT INFO
# Provides: jenkins-slave
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Jenkins Slave
# Description: This file starts and stops connection of Jenkins Slave
#
### END INIT INFO
case “$1” in
start)
sudo java -jar slave.jar -jnlpUrl http://&lt; jenkins_url >:< port >/computer/< jenkins node name >/slave-agent.jnlp -jnlpCredentials < username >:< password >
;;
stop)
;;
restart)
sudo java -jar slave.jar -jnlpUrl http://&lt; jenkins_url >:< port >/computer/< jenkins node name >/slave-agent.jnlp -jnlpCredentials < username >:< password >
;;
*)
echo “Usage: jenkins-slave {start|stop|restart}” >&2
exit 3
;;
esac

NOTE: ‘#!’ in the first line tells that it will be run as root user

5. Save the file and returned to terminal mode from editor.
6. After that ran the update-rc.d command to add the script to startup
root@ip-***-**-**-**:/etc/init.d# update-rc.d jenkins-slave defaults
7. After command ran successfully I rebooted the machine.
8. What!!! it didn’t work. I verified it with the Jenkins Node, that was still showing not connected!
9. I Stopped my AWS VM and the Started it again.
10. Urgh….. No luck this time also

So that was our first attempt so run the command as service. We Failed??? May be or may not be 🙂 This failure boosted my motivation to solve the problem. Next day I pinged my colleague again but for a release he was too busy to sit again. So I decided to move on, this time All Alone 🙂 As stated above, this time I sat with the solutions I got from different sources.

Now I decided to check the System Log of the AWS VM for my instance, which was the first TURING point for the solution. For checking the system log I did:

  • Open EC2 Manage Console of AWS
  • Goto Instances page
  • Select my instance
  • From Actions dropdown menu select: Instance Settings > Get System Log
  • Walla…. Got the System Log

When I checked the log I found that it is showing the following error:

Error: Unable to access slave.jar

Hmm… that’s weird. So I figure out that I should download the slave.jar and then run the connection command. So I downloaded it and give proper permission and also updated the connection command to reflect the location of the slave.jar file. Now restarted the VM….. Sigh… No luck again….

Check the system log again and found the same command. There is something fishy going on with the slave.jar permission, so I deleted the slave.jar:

root@ip-***-**-**-**:# rm slave.jar

So I was thinking that whatever it is I need the slave.jar first in order to connect my slave with master.idea

TADAAAA…. Got the idea, why not add a download command for slave.jar before the connection command? So I immediately added the following line:

sudo wget < URL for slave.jar >

Side NOTE: You can get the url simply by:

  1. Goto Jenkins and open Jenkins Node
  2. As slave is not connected it will show some connection commands
  3. In the command for headless slave, you can see that the slave.jar is a hyperlink.
  4. Right click on it and get the url.
  5. You are DONE

So I saved the file and ran the update-rc.d command again. This time it shows that this is already added. Do not worry about the message. I rebooted the system but….. again it didn’t work. This time I felt that if a thermometer would have place over my head that it would surely burst out due to over temperature. I asked myself WHY its not working now? So I moved to /etc/init.d and checked the permission of jenkins-slave

root@ip-***-**-**-**:/etc/init.d# ls -la

The permission is not satisfying… it showed something like –rw-r-x-x–. So I updated the permission

root@ip-***-**-**-**:/etc/init.d# chmod a+w jenkins-slave

Now I re-run the update-rc.d, it was not needed even though. Got the message again telling that it is added already. That’s OK I just wanted to double sure that this is attached to startup service. Now I rebooted the system.

Yahoooooooo…… I worked now. Felt a big relief and one of my project was hanging at the edge just for this simple thing 🙂 I am listing the solution below so that it becomes easy to get it right away:

Solution for adding Jenkins slave connection command to run at startup at Ubuntu 14.04 AWS:

1. Login to the VM
2. Switch to root user
$ sudo -s
3. Goto /etc/init.d
# cd /etc/init.d
4. Create a file for your service (for example using jenkins-slave, you can give any name as you like)
# nano jenkins-slave
5. Enter the following:

#! /bin/sh
### BEGIN INIT INFO
# Provides: jenkins-slave
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Jenkins Slave
# Description: This file starts and stops connection of Jenkins Slave
#
### END INIT INFO
case “$1” in
start)

sudo wget < url_for_slave.jar (please see above for how you can get this link) >
sudo java -jar slave.jar -jnlpUrl http://&lt; jenkins_url >:< port >/computer/< jenkins node name >/slave-agent.jnlp -jnlpCredentials < username >:< password >
;;
stop)
;;
restart)

sudo wget < url_for_slave.jar (please see above for how you can get this link) >
sudo java -jar slave.jar -jnlpUrl http://&lt; jenkins_url >:< port >/computer/< jenkins node name >/slave-agent.jnlp -jnlpCredentials < username >:< password >
;;
*)
echo “Usage: jenkins-slave {start|stop|restart}” >&2
exit 3
;;
esac

6. Save and close file
7. Change the permission of jenkins-slave
# chmod a+x jenkins-slave
8. Run command
# update-rc.d <script-file-name> defaults
9. Reboot the system. You can also Stop and Start your AWS instance.
10. Check your Jenkins Node, it should be connected now 🙂

Hope this will help you 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s