Created
November 8, 2018 15:32
-
-
Save holin/d725a7adbc5df4119ad0a4498f00281f to your computer and use it in GitHub Desktop.
init.d
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
#!/usr/bin/env bash | |
### BEGIN INIT INFO | |
# Provides: wechat_message_bot | |
# Required-Start: $all | |
# Required-Stop: $network $local_fs $syslog | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Start the wechat_message_bots at boot | |
# Description: Enable at boot time. | |
### END INIT INFO | |
__sig() | |
{ | |
typeset __pid | |
[[ -s "$2" ]] && | |
__pid="$(cat "$2")" && | |
[[ -n "${__pid}" ]] && | |
kill -$1 "${__pid}" >/dev/null 2>&1 || | |
return $? | |
} | |
sig() | |
{ | |
__sig "$1" "$PID" || return $? | |
} | |
sig_bot() | |
{ | |
__sig "$1" "$PID_BOT" || return $? | |
} | |
oldsig() | |
{ | |
__sig "$1" "$OLD_PID" || return $? | |
} | |
run() | |
{ | |
echo -n "$1 - " | |
shift | |
if | |
"$@" | |
then | |
echo "OK" | |
else | |
typeset result=$? | |
echo "Failed!" >&2 | |
return $result | |
fi | |
} | |
prefix_command_with_su_fix_quoting() | |
{ | |
typeset -a __temp | |
__temp=() | |
while | |
(( $# )) | |
do | |
__temp+=( "'$1'" ) | |
shift | |
done | |
CMD=( su - "${__owner}" -c "cd '$RACK_ROOT' && ${__temp[*]}" ) | |
} | |
setup () | |
{ | |
export RACK_ROOT=/home/app/site/wechat_message_bot | |
echo "$RACK_ROOT: " | |
cd $RACK_ROOT || return $? | |
export PID=$RACK_ROOT/wechat_message_web.pid | |
export PID_BOT=$RACK_ROOT/wechat_message_bot.pid | |
export OLD_PID="$PID.oldbin" | |
export APP_ENV=production | |
CMD=( /home/app/.rvm/wrappers/wechat_message_bot/rackup -E "${APP_ENV}" -o 0.0.0.0 -p 9292 -P "${PID}" -D ) | |
typeset __owner="$(stat -c "%U" "${RACK_ROOT}")" | |
if | |
[[ "${USER:=$(whoami)}" == "${__owner}" ]] | |
then | |
true # it's all fine we run as owner of the app | |
elif | |
(( UID == 0 )) | |
then | |
prefix_command_with_su_fix_quoting "${CMD[@]}" | |
else | |
echo "ERROR: running not as owner(${__owner}) of '$RACK_ROOT' and not as root($USER), prefix with 'sudo' and try again!" | |
return 2 | |
fi | |
} | |
cmd_start() | |
{ | |
if sig 0 | |
then echo "Already started" | |
else run "Starting" "${CMD[@]}" || return $? | |
fi | |
} | |
wait_pid_kill() | |
{ | |
typeset __count=$1 | |
while | |
(( __count > 0 )) && | |
sig 0 && sig_bot 0 | |
do | |
: $(( __count-- )) | |
sleep 1s | |
done | |
sig 0 && sig_bot 0 || return $? | |
} | |
cmd_stop() | |
{ | |
run "Stopping web" sig QUIT | |
run "Stopping bot" sig_bot QUIT | |
if | |
wait_pid_kill 5 | |
then | |
run "Force stop web" sig TERM | |
run "Force stop bot" sig_bot TERM | |
if wait_pid_kill 3 | |
then return 1 | |
fi | |
fi | |
} | |
cmd_restart() | |
{ | |
cmd_stop && cmd_start || return $? | |
} | |
cmd_reload() | |
{ | |
run "Reloading" sig USR2 && | |
wait_pid_kill 5 && | |
oldsig QUIT || | |
oldsig TERM || | |
cmd_restart || | |
return $? | |
} | |
cmd_rotate() | |
{ | |
run "Rotate" sig USR1 || | |
cmd_start || | |
return $? | |
} | |
cmd() | |
{ | |
setup || return $? | |
case "$1" in | |
start|stop|restart|reload|rotate) | |
cmd_$1 || return $? | |
;; | |
upgrade) | |
cmd_reload || return $? | |
;; | |
*) | |
echo "Usage: $0 <start|stop|restart>" >&2 | |
return 1 | |
;; | |
esac | |
} | |
cmd "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment