-
-
Save mauriciopasquier/7e32290d13453dfc5b1c7ae873a92b40 to your computer and use it in GitHub Desktop.
Sample /etc/init.d script for running GeoServer as a service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
### | |
# Adapted from https://gist.github.com/dancingfrog/97eb466016d096bf9e704254c6d84738 | |
### | |
NAME=geoserver | |
SERVICE_NAME=GeoServer | |
GEOSERVER_HOME=/usr/share/geoserver/geoserver-2.12.1 | |
START=$GEOSERVER_HOME/bin/startup.sh | |
STOP=$GEOSERVER_HOME/bin/shutdown.sh | |
RUNAS=root | |
JAVA_HOME=/usr/lib/jvm/java-1.8.0 | |
PIDFILE=/var/run/$NAME.pid | |
LOGFILE=/var/log/$NAME.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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment