Hudson Service script
January 2nd, 2009Below is the code for startHudson.sh
#!/usr/bin/env bash
# Uncomment the following line if you have OutOfMemoryError errors
# HUDSON_OPTS=”-Xms128m -Xmx256m”export JAVA_HOME=/usr/java/default
LAUNCHER=hudson.warEXEC=”$JAVA_HOME/bin/java $HUDSON_OPTS -jar $LAUNCHER $@ ”
echo $EXEC
$JAVA_HOME/bin/java $HUDSON_OPTS -jar “$LAUNCHER” $@ &
echo $! > hudson.pid
Below this is the code for hudson.sh which should then be symlinked into /etc/init.d/hudson , then it should be chkconfig –add hudson to make it a service.
#!/bin/sh
#content of /opt/hudson/default/hudson.sh script
# chkconfig: 345 99 05
# description: Hudson build loop# Hudson Unix Startup Script Version 1.0
#
# based on http://confluence.public.thoughtworks.org/display/CC/UnixStartupScriptVersion1.x
# adapted for multiple projects
# also based on the file attached to the above page created by Jerome Lacoste#
# Hudson startup: Startup and kill script for Hudson
#
###################################################################################################
# USER CONFIGURATION
#
# Fill in these values for your Hudson setup# What user will Hudson run as? The user will need permission to write and modify files
# in the next entries.
HUDSON_USER=hudson# Where is the HUDSON startup script located?
HUDSON_INSTALL_DIR=/opt/hudson/default# In what directory is the config.xml file located for HUDSON?
# default: HUDSON_WORK_DIR=$HUDSON_INSTALL_DIR
HUDSON_WORK_DIR=$HUDSON_INSTALL_DIR# Where will the hudson.log file be located?
# default: HUDSON_LOGFILE_DIR=$HUDSON_INSTALL_DIR
HUDSON_LOGFILE_DIR=$HUDSON_INSTALL_DIR#######################
# ENVIRONMENT ADDITIONS# Add environement variables here that are needed
# example:
# export JAVA_HOME=/usr/local/java
#
export JAVA_HOME=/usr/java/default
export JDK_HOME=/usr/java/default
export ANT_HOME=/opt/ant/default
export M2_HOME=/opt/maven/default
export M2=$M2_HOME/bin
export GROOVY_HOME=/opt/groovy/default
export HUDSON_HOME=/opt/hudson/default# Add path to additional executables needed for project build. See PATH entry below for base config.
# No additional action taken when blank.
PATH_ADDITIONS=##############################
# Hudson PORT SETTINGS# Port for application. You can access it by going to http://localhost:8080
# Specify only one either Webport or Securewebport if you want to use https
# default HUDSON_WEBPORT=8080
HUDSON_WEBPORT=8080
HUDSON_SECURE_WEBPORT=###################################################################################################
# DO NOT MODIFY ENTRIES BELOW THIS LINENAME=hudson
DESC=”Hudson - continuous integration build loop”PATH=/sbin:/usr/sbin:/usr/bin:/bin
# add additions if variable has text defined
if [ -n “$PATH_ADDITIONS” ]; then
PATH=$PATH_ADDITIONS:$PATH
fi
export PATHHUDSON_DAEMON=$HUDSON_INSTALL_DIR/startHudson.sh
HUDSON_LOG_FILE=$HUDSON_LOGFILE_DIR/hudson.log
if [ -n “$HUDSON_SECURE_WEBPORT” ]; then
HUDSON_COMMAND=”$HUDSON_DAEMON –httpsPort=$HUDSON_SECURE_WEBPORT –webroot=/opt/hudson/default/hudson ”
fiif [ -n “$HUDSON_WEBPORT” ]; then
HUDSON_COMMAND=”$HUDSON_DAEMON –httpPort=$HUDSON_WEBPORT –webroot=/opt/hudson/default/hudson ”
fi# does the executable exist?
test -f $HUDSON_DAEMON || (echo “The executable $HUDSON_DAEMON does not exist!” && exit 0)if [ `id -u` -ne 0 ]; then
echo “Not starting/stopping $DESC, you are not root.”
exit 4
fi# Get the PID output from the startup script
if [ -f $HUDSON_INSTALL_DIR/hudson.pid ]; then
HUDSON_PID=`cat $HUDSON_INSTALL_DIR/hudson.pid`
else
echo “No hudson.pid file found. HUDSON process may not be controllable from this script!”
ficase “$1″ in
’start’)
cd $HUDSON_INSTALL_DIR
#echo “HUDSON environtment at startup” > hudson.startup.env
#env >> hudson.startup.env
su $HUDSON_USER -c “/bin/sh -c \”$HUDSON_COMMAND >> $HUDSON_LOG_FILE 2>&1\”" & RETVAL=$?
echo “$NAME started on port ${HUDSON_WEBPORT}”
;;’stop’)
if [ -n “$HUDSON_PID” ] && ps -p ${HUDSON_PID} > /dev/null ; then
kill -9 ${HUDSON_PID}
$0 status
RETVAL=$?
else
echo “$NAME is not running”
RETVAL=1
fi
;;’status’)
if [ -n “$HUDSON_PID” ] && ps -p ${HUDSON_PID} > /dev/null ; then
echo $NAME \(pids $HUDSON_PID\) is running
RETVAL=0
else
echo “$NAME is stopped”
RETVAL=1
fi
;;‘restart’)
$0 stop && $0 start
RETVAL=$?
;;*)
echo “Usage: $0 { start | stop | status | restart }”
exit 1
;;
esac
#echo ending $0 $$….
exit 0;