diff --git a/README.md b/README.md index 80f9906..dff01a2 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,8 @@ * v0.2.0 - add restart command * v0.1.0 - first version + +* Service + * https://bitbucket.org/hatterjiang/linux-service-sample/src/master/ + * https://stackoverflow.com/questions/61443052/rust-daemon-best-practices + diff --git a/install-service.sh b/install-service.sh new file mode 100644 index 0000000..b6def5d --- /dev/null +++ b/install-service.sh @@ -0,0 +1,97 @@ +#! /bin/bash + +export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin + +install_path=/keeprunning-service/ +service_name='keeprunning-service' + +# Make sure only root can run our script +function rootness(){ + if [[ $EUID -ne 0 ]]; then + echo "Error:This script must be run as root!" 1>&2 + exit 1 + fi +} + +function checkenv(){ + if [[ $OS = "centos" ]]; then + echo "Environment: CentOS" + #yum install -y java + else + echo "Environment: Debian or Ubuntu" + #apt-get install -y openjdk-7-jre + fi +} + +function checkos(){ + if [ -f /etc/redhat-release ];then + OS='centos' + elif [ ! -z "`cat /etc/issue | grep bian`" ];then + OS='debian' + elif [ ! -z "`cat /etc/issue | grep Ubuntu`" ];then + OS='ubuntu' + else + echo "Not support OS, Only supports CentOS, Debian and Ubuntu!" 1>&2 + exit 1 + fi +} + +# Install service +function install_service(){ + rootness + checkos + checkenv + mkdir -p $install_path + cp sample-service-jar.jar $install_path + cp install-service.sh $install_path + cp $service_name /etc/init.d/ + if [ "$OS" == 'centos' ]; then + chmod +x /etc/init.d/$service_name + chkconfig --add $service_name + chkconfig $service_name on + else + chmod +x /etc/init.d/$service_name + update-rc.d -f $service_name defaults + fi + #/etc/init.d/$service_name start +} + +# Uninstall service +function uninstall_service(){ + printf "Are you sure uninstall $service_name? (y/n) " + printf "\n" + read -p "(Default: n):" answer + if [ -z $answer ]; then + answer="n" + fi + if [ "$answer" = "y" ]; then + /etc/init.d/$service_name stop + checkos + if [ "$OS" == 'centos' ]; then + chkconfig --del $service_name + else + update-rc.d -f $service_name remove + fi + rm -f /etc/init.d/$service_name + rm -rf $install_path + echo "$service_name uninstall success!" + else + echo "uninstall cancelled, Nothing to do" + fi +} + +# Initialization step +action=$1 +[ -z $1 ] && action=install +case "$action" in +install) + install_service + ;; +uninstall) + uninstall_service + ;; +*) + echo "Arguments error! [${action} ]" + echo "Usage: `basename $0` {install|uninstall}" + ;; +esac diff --git a/keeprunning-service b/keeprunning-service new file mode 100644 index 0000000..12ddc4f --- /dev/null +++ b/keeprunning-service @@ -0,0 +1,86 @@ +#!/bin/bash +# chkconfig: 2345 90 10 +# description: start or stop the keeprunning-service server +# +### BEGIN INIT INFO +# Provides: keeprunning-service +# Required-Start: $network $syslog $local_fs $remote_fs +# Required-Stop: +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Description: Start or stop the keeprunning-service server +### END INIT INFO + +# Author: hatter + +export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin + +name=keeprunning-service +install_path=/keeprunning-service/ +fnname=keeprunningd +logname=${install_path}service.log + +[[ -s "/java-env-init.sh" ]] && source "/java-env-init.sh" +[[ -s "${install_path}env-init.sh" ]] && source "${install_path}env-init.sh" + +start(){ + ulimit -s 65535 + ulimit -n 65535 + $install_path$fnname > $logname 2>&1 & + RETVAL=$? + if [ "$RETVAL" = "0" ]; then + echo "$name start success" + else + echo "$name start failed" + fi +} + +stop(){ + pid=`ps -ef | grep $fnname | grep -v grep | awk '{print $2}'` + if [[ ! -z $pid ]]; then + ps -ef | grep $fnname | grep -v grep | awk '{print $2}' | xargs kill -s 9 + RETVAL=$? + if [ "$RETVAL" = "0" ]; then + echo "$name stop success" + else + echo "$name stop failed" + fi + else + echo "$name is not running" + RETVAL=1 + fi +} + +status(){ + pid=`ps -ef | grep $fnname | grep -v grep | awk '{print $2}'` + if [[ -z $pid ]]; then + echo "$name is not running" + RETVAL=1 + else + echo "$name is running with PID $pid" + RETVAL=0 + fi +} + +case "$1" in +'start') + stop + start + ;; +'stop') + stop + ;; +'status') + status + ;; +'restart') + stop + start + RETVAL=$? + ;; +*) + echo "Usage: $0 { start | stop | restart | status }" + RETVAL=1 + ;; +esac +exit $RETVAL \ No newline at end of file