linux 可执行 程序做服务

查看服务:chkconfig --list

制作服务

#!/bin/sh
# chkconfig: 345 99 10
# description: Auto-starts tomcat
# /etc/init.d/tomcatd
# Tomcat auto-start
# Source function library.
#. /etc/init.d/functions
# source networking configuration.
#. /etc/sysconfig/network
RETVAL=0
#根据自己的地址进行设置
export JAVA_HOME=/usr/local/jdk1.8.0_201
export JRE_HOME=/usr/local/jdk1.8.0_201/jre
export CATALINA_HOME=/opt/apache-tomcat-8.5.30
export CATALINA_BASE=/opt/apache-tomcat-8.5.30
 
start()
{
    TOMCAT_PID=`pidof java`
    if [[ -n $TOMCAT_PID ]]
        then
        echo "Tomcat is running, pid is $TOMCAT_PID."
    else    
            if [ -f $CATALINA_HOME/bin/startup.sh ];
              then
                    echo $"Starting Tomcat"
                    $CATALINA_HOME/bin/startup.sh
                    RETVAL=$?
                    echo " OK"
        fi
        fi
        return $RETVAL
}
stop()
{
        if [ -f $CATALINA_HOME/bin/shutdown.sh ];
          then
            echo $"Stopping Tomcat"
                $CATALINA_HOME/bin/shutdown.sh
            RETVAL=$?
            sleep 1
            ps -fwwu root | grep tomcat|grep -v grep | grep -v PID | awk {print $2}|xargs kill -9
            echo " OK"
            # [ $RETVAL -eq 0 ] && rm -f /var/lock/...
            return $RETVAL
        fi
}
 
status()
{
    TOMCAT_PID=`pidof java`
    if [[ -n $TOMCAT_PID ]]
        then
        echo "Tomcat is running, pid is $TOMCAT_PID."
    else    
            echo "Tomcat is not running..."
        fi
}
case "$1" in
 start) 
        start
        ;;
 stop)  
        stop
        ;;
 status)
    status
    ;;                                               
 restart)
         echo $"Restaring Tomcat"
         $0 stop
         sleep 1
         $0 start
         ;;
 *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $RETVAL

将程序放到:/etc/init.d

添加服务:chkconfig --add tomcat

查看

tomcat             0:off    1:off    2:on    3:on    4:on    5:on    6:off

 

linux 可执行 程序做服务

上一篇:linux-sed


下一篇:linux监控命令