Skip to content

Instantly share code, notes, and snippets.

@mcgivrer
Created November 9, 2012 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcgivrer/4042943 to your computer and use it in GitHub Desktop.
Save mcgivrer/4042943 to your computer and use it in GitHub Desktop.
A small script to start/stop all services for a Continuous Integration Server.
#!/bin/bash
# Small script to start/stop a
# Continuous Integration server based on ubuntu 12.04.
# inspired by sonar service script.
# (copyleft) 2012 - McGivrer
#
checkUser() {
# $1 touchLock flag
# $2 command
# Check the configured user. If necessary rerun this script as the desired user.
if [ "X$RUN_AS_USER" != "X" ]
then
# Resolve the location of the 'id' command
IDEXE="/usr/xpg4/bin/id"
if [ ! -x "$IDEXE" ]
then
IDEXE="/usr/bin/id"
if [ ! -x "$IDEXE" ]
then
echo "Unable to locate 'id'."
echo "Please report this message along with the location of the command on your system."
exit 1
fi
fi
if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ]
then
# Already running as the configured user. Avoid password prompts by not calling su.
RUN_AS_USER=""
fi
fi
if [ "X$RUN_AS_USER" != "X" ]
then
# If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be
# able to create the lock file. The Wrapper will be able to update this file once it
# is created but will not be able to delete it on shutdown. If $2 is defined then
# the lock file should be created for the current command
if [ "X$LOCKPROP" != "X" ]
then
if [ "X$1" != "X" ]
then
# Resolve the primary group
RUN_AS_GROUP=`groups $RUN_AS_USER | awk '{print $3}' | tail -1`
if [ "X$RUN_AS_GROUP" = "X" ]
then
RUN_AS_GROUP=$RUN_AS_USER
fi
touch $LOCKFILE
chown $RUN_AS_USER:$RUN_AS_GROUP $LOCKFILE
fi
fi
# Still want to change users, recurse. This means that the user will only be
# prompted for a password once. Variables shifted by 1
su -m $RUN_AS_USER -c "\"$REALPATH\" $2"
RETVAL=$?
# Now that we are the original user again, we may need to clean up the lock file.
if [ "X$LOCKPROP" != "X" ]
then
getpid
if [ "X$pid" = "X" ]
then
# Wrapper is not running so make sure the lock file is deleted.
if [ -f "$LOCKFILE" ]
then
rm "$LOCKFILE"
fi
fi
fi
exit $RETVAL
fi
}
# Start all services for CI Box.
start() {
sudo service tomcat7 start
sudo service sonar start
sudo service jenkins start
}
# Stop all services for CI Box.
stop() {
sudo service jenkins stop
sudo service sonar stop
sudo service tomcat7 stop
}
# Get Status for all CI Box services.
status() {
sudo service jenkins status
sudo service sonar status
sudo service tomcat7 status
}
case "$1" in
'start')
checkUser touchlock $1
start
;;
'stop')
checkUser "" $1
stop
;;
'restart')
checkUser touchlock $1
stop
start
;;
'status')
checkUser "" $1
status
;;
*)
echo "Usage: $0 { start | stop | restart | status }"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment