Skip to content

Instantly share code, notes, and snippets.

@dancingfrog
Forked from naholyr/_service.md
Last active September 26, 2022 22:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save dancingfrog/97eb466016d096bf9e704254c6d84738 to your computer and use it in GitHub Desktop.
Save dancingfrog/97eb466016d096bf9e704254c6d84738 to your computer and use it in GitHub Desktop.
Sample /etc/init.d script for running GeoServer as a service
#!/bin/sh
### BEGIN INIT INFO
# Provides: GeoServer
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Starts and stops the GeoServer, which should be located at /usr/share/geoserver
### END INIT INFO
SERVICE_NAME=GeoServer
START=/usr/share/geoserver/bin/startup.sh
STOP=/usr/share/geoserver/bin/shutdown.sh
RUNAS=root
GEOSERVER_HOME=/usr/share/geoserver
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre
PIDFILE=/var/run/geoserver.pid
LOGFILE=/var/log/geoserver.log
start() {
if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
echo "$SERVICE_NAME is already running" >&2
return 1
fi
echo "Starting $SERVICE_NAME…" >&2
su -c "GEOSERVER_HOME=$GEOSERVER_HOME JAVA_HOME=$JAVA_HOME $START &> '$LOGFILE' & echo \$!" $RUNAS > "$PIDFILE"
echo "$SERVICE_NAME is started" >&2
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo "$SERVICE_NAME is not running" >&2
if [ -f "$PIDFILE" ]; then
echo "Removing $PIDFILE"
rm $PIDFILE
fi
return 1
fi
echo "Stopping $SERVICE_NAME…" >&2
su -c "$STOP &> '$LOGFILE' & echo \$!" $RUNAS && kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo "$SERVICE_NAME has stopped" >&2
}
status() {
printf "%-50s" "Checking $SERVICE_NAME..."
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
printf "%s\n" "Process dead but pidfile exists"
exit 1
else
echo "$SERVICE_NAME is running"
fi
else
printf "%s\n" "$SERVICE_NAME is not running"
exit 3
fi
}
case "$1" in
status)
status
;;
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|restart|status|stop}"
esac
exit 0
@bryaneaton
Copy link

Thanks for this!

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