PEAT Online Course -
Test-Driven Development
using Kotlin (DELUXE EDITION)

  • Theory 'n' practice
  • Test-driven method
  • Acceptance criteria
  • Learn to code mastery
  • Ace that job interview
  • Free updates
AVAILABLE NOW
VIMEO ON-DEMAND

Ubuntu ActiveMQ Initialisation Scripts

30 March 2012 Comments off

3 minutes

617

Recently, I have been working with Ubuntu in a Virtual Box VM and setting up ActiveMQ for testing. I wanted to create a VM with an already running ActiveMQ as a service, whenever I started it. In order to do this, I set up ActiveMQ as a Linux intialisation service. I installed ActiveMQ first by following the very helpful blog entry here, which was a little out of date. It only explained how to create an ActiveMQ Init.d if you have Sys V Linux/Unix machine. So I went about customising the information for a Ubuntu machine, and the results are now here. Here are my scripts, the first is located at /home/activemq/activemqstart.sh. This script launches the service and is  owned by the Linux user activemq.

</pre>
#!/bin/bash # Start ActiveMQ if [ ! -f ${HOME}/activemq.env ]; then echo "Cannot read activemq.env" 1>&2; exit 99 fi . ${HOME}/activemq.env ${ACTIVEMQ_HOME}/bin/activemq start # fini
<pre>

Here is the script file /home/activemq/activemqstatus.sh, which shows the administrator the status of the service.

</pre>
#!/bin/bash # Probe ActiveMQ if [ ! -f ${HOME}/activemq.env ]; then echo "Cannot read activemq.env" 1>&2; exit 99 fi . ${HOME}/activemq.env ${ACTIVEMQ_HOME}/bin/activemq status # fini
<pre>

Here is the script file /home/activemq/activemqstop.sh, which allows the administrator to halt the service.

</pre>
#!/bin/bash # Stop ActiveMQ if [ ! -f ${HOME}/activemq.env ]; then echo "Cannot read activemq.env" 1>&2; exit 99 fi . ${HOME}/activemq.env ${ACTIVEMQ_HOME}/bin/activemq stop # fini
<pre>

Here is the environmental set-up script /home/activemq/activemq.env. Of course, if you wanted to, you could put this configuration elsewhere like in a .bashrc script. For my purposes, I wanted to refactor out the location of both components.

</pre>
: ${JAVA_HOME:=/usr/lib/jvm/java-6-sun-1.6.0.31} : ${JDK_HOME:=/usr/lib/jvm/java-6-sun-1.6.0.31} : ${ACTIVEMQ_HOME:=/opt/apache-activemq}
<pre>

Here is a version of ActiveMQ control script in /etc/init.d/activemq, which owned by the superuser root.

#!/bin/bash
#
# activemq       Starts ActiveMQ.
#
#
### BEGIN INIT INFO
# Provides:          activemq
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     1 2 3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by Apache Active MQ.
### END INIT INFO

# Source function library.
## . /etc/init.d/functions

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

[ -f /home/activemq/activemqstart.sh ] || exit 0
[ -f /home/activemq/activemqstop.sh ] || exit 0
[ -f /home/activemq/activemqstatus.sh ] || exit 0

RETVAL=0

umask 077

start() {
       echo -n $"Starting ActiveMQ: "
       su -c /home/activemq/activemqstart.sh activemq
       echo
       return $RETVAL
}
stop() {
       echo -n $"Shutting down ActiveMQ: "
       su -c /home/activemq/activemqstop.sh activemq
       echo
       return $RETVAL
}
status() {
       echo -n $"Probing status of ActiveMQ: "
       su -c /home/activemq/activemqstatus.sh activemq
       echo
       return $RETVAL
}
restart() {
       stop
       start
}
case "$1" in
 start)
       start
       ;;
 stop)
       stop
       ;;
 restart|reload)
       restart
       ;;
 status|probe)
        status
        ;;
 *)
       echo $"Usage: $0 {start|stop|restart|status|probe}"
       exit 1
esac

exit $?
# Fini

I will assume that you are familiar with creating Linux users and setting file owner and group permissions. Finally, I installed the new service with this following command:

sudo update-rc.d activemq start 20 1 2 3 4 5 . stop 80 0 1 2 6 .

This command installs the service for single-user, multi-user, network configured and display configured run-levels. HTH. Al Coda. 😉

Hey all! Thanks for visiting. I provide fringe benefits to interested readers: checkout consultancy, training or mentorship Please make enquiries by email or
call +44 (0)7397 067 658.

Due to the Off-Payroll Working plan for the UK government, I am enforcing stricter measures on contracts. All potential public sector GOV.UK contracts engagements must be approved by QDOS and/or SJD Accounting. Please enquire for further information.