#! /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