feat: add service
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
97
install-service.sh
Normal file
97
install-service.sh
Normal file
@@ -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
|
||||
86
keeprunning-service
Normal file
86
keeprunning-service
Normal file
@@ -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 <https://playsecurity.org/>
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user