Created
August 15, 2018 06:04
-
-
Save weldpua2008/8522a9c71ef6a5a15a8e39ecb1e95aaa to your computer and use it in GitHub Desktop.
Example init script for Go Service (SystemV)
This file contains hidden or 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 | |
# | |
# GO - this script starts and stops the Event server daemon | |
# | |
# chkconfig: 345 85 15 | |
# description: GO - server written in golang | |
# processname: go | |
# config: /config/go.json | |
# pidfile: /var/run/go.pid | |
# Source function library. | |
. /etc/rc.d/init.d/functions | |
# Source networking configuration. | |
. /etc/sysconfig/network | |
# Check that networking is up. | |
[ "$NETWORKING" = "no" ] && exit 0 | |
go="/bin/go-server" | |
prog=$(basename $go) | |
pidfile=/var/run/go.pid | |
GO_USER="flume" | |
CONF_FILE="/config/go.json" | |
if [ -f /proc/cpuinfo ]; then | |
threads=$(cat /proc/cpuinfo | grep -i 'core id' | wc -l) | |
elif [ -d /System/Library ]; then | |
threads=$(sysctl machdep.cpu.thread_count | cut -f2 -d\ ) | |
else | |
threads=8 | |
fi | |
[[ ${threads} -gt 1 ]] && threads=$((threads / 5)) | |
[[ ${threads} -ge 1 ]] || threads=1 | |
GOMAXPROCS="${threads:-1}" | |
[ -f /etc/sysconfig/go ] && . /etc/sysconfig/go | |
lockfile=/var/lock/subsys/go.events | |
start() { | |
if [ ! -x $go ];then | |
failure $"$go has not permissions" | |
exit 5 | |
fi | |
if [ ! -e "$CONF_FILE" ];then | |
failure $"$CONF_FILE is not exist" | |
exit 6 | |
fi | |
echo -n $"Starting $prog: " | |
runuser -s /bin/bash ${GO_USER} -c "GOMAXPROCS=${GOMAXPROCS:-10} $go -c $CONF_FILE" >/dev/null 2>&1 & | |
retval=$? | |
_pid=$! | |
if [ $retval -eq 0 ];then | |
touch $lockfile | |
mkdir -p $(dirname "$pidfile") | |
[ "$_pid" != "" ] && echo "$_pid" > $pidfile | |
success $"$prog startup" | |
else | |
failure $"exit code $retval" | |
fi | |
echo | |
return $retval | |
} | |
stop() { | |
echo -n $"Stopping $prog: " | |
declare -f checkpid &> /dev/null && _checkpid_exist="true" || _checkpid_exist="false" | |
retval=$? | |
checkpid $(cat "$pidfile" 2> /dev/null) | |
if [[ $? -eq 0 ]] || [[ "${_checkpid_exist:-false}" = "false" ]];then | |
killproc -p $pidfile $go -QUIT | |
retval=$? | |
fi | |
echo | |
[ $retval -eq 0 ] && rm -f $lockfile | |
return $retval | |
} | |
restart() { | |
configtest || return $? | |
stop | |
sleep 3 | |
start | |
} | |
force_reload() { | |
restart | |
} | |
configtest() { | |
RETVAL=1 | |
echo -n $"Checking $CONF_FILE: " | |
if [[ -e "$CONF_FILE" ]];then | |
cat "$CONF_FILE" 2> /dev/null | jq . &> /dev/null | |
RETVAL=$? | |
fi | |
return $RETVAL | |
} | |
rh_status() { | |
status -p $pidfile $go | |
} | |
rh_status_q() { | |
rh_status >/dev/null 2>&1 | |
} | |
case "$1" in | |
start) | |
rh_status_q && exit 0 | |
$1 | |
;; | |
stop) | |
rh_status_q || exit 0 | |
$1 | |
;; | |
restart|configtest) | |
$1 | |
;; | |
reload) | |
rh_status_q || exit 7 | |
$1 | |
;; | |
force-reload) | |
force_reload | |
;; | |
status) | |
rh_status | |
;; | |
condrestart|try-restart) | |
rh_status_q || exit 0 | |
;; | |
*) | |
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" | |
exit 2 | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment