美文网首页
linux各服务启动脚本【nginx、tomcat、kafka、

linux各服务启动脚本【nginx、tomcat、kafka、

作者: 哆来咪发都不会 | 来源:发表于2019-08-01 17:51 被阅读0次

    服务器中服务众多,每次重启服务器、或者服务器宕机。都需要重新启动服务,费时耗力。因此想到将所有的服务全部干成系统服务,并开机自启。

    以下操作均在centos6中进行

    整体流程如下:

    1. 编写启动脚本

    2.将脚本设置为系统服务

    chkconfig --add script_name
    chkconfig script_name on
    

    3.验证是否已开机自启

    chkconfig --list script_name
    script_name          0:off  1:off   2:on    3:on    4:on    5:on    6:off
    

    0-6代表启动7种运行级别,脚本中可设置

    以上内容网络上很多,也更加详细,各位有兴趣可自行网上查阅。

    以下附件一篇较详细的文章:

    https://www.jianshu.com/p/635e8480a75e

    下面附上各服务的启动脚本:

    tomcat脚本

    #!/bin/bash
    #program:
    #this program is tomcat startup script for the Tomcat server
    #chkconfig: 2345 90 10
    #Author: hhhhuanzi
    #Date:  2019-07-12
    
    source /etc/profile
    
    JAVA_HOME=$JAVA_HOME
    CATALINA_HOME=$CATALINA_HOME
    
    case "$1" in
    start)
            echo -n "Starting Tomcat..."
            $CATALINA_HOME/bin/startup.sh
            exit 0
            ;;
    stop)
            echo -n "Stopping Tomcat..."
            $CATALINA_HOME/bin/shutdown.sh
            echo "Stopped"
            exit 0
            ;;
    restart)
            echo -n "Stopping Tomcat..."
            $CATALINA_HOME/bin/shutdown.sh
            echo "Stopped."
            sleep 2
            echo -n "Starting Tomcat..."
            $CATALINA_HOME/bin/startup.sh
            echo "Started."
            exit 0
            ;;
    *)
            echo "Usage:tomcat{start|stop|restart}"
            exit 1
            ;;
      
    esac
    

    nginx脚本

    #!/bin/sh
    #
    # nginx - this script starts and stops the nginx daemon
    #
    # chkconfig:   - 85 15 
    # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
    #               proxy and IMAP/POP3 proxy server
    # processname: nginx
    # config:      /usr/local/nginx/conf/nginx.conf 
    # pidfile:     /var/run/nginx.pid
     
    # Source function library.
    . /etc/rc.d/init.d/functions
     
    # Source networking configuration.
    . /etc/sysconfig/network
     
    # Check that networking is up.
    [ "$NETWORKING" = "no" ] && exit 0
     
    nginx="/usr/local/nginx/sbin/nginx"
    prog=$(basename $nginx)
     
    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
     
    [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
     
    lockfile=/var/lock/subsys/nginx
     
    make_dirs() {
       # make required directories
       user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
       if [ -z "`grep $user /etc/passwd`" ]; then
           useradd -M -s /bin/nologin $user
       fi
       options=`$nginx -V 2>&1 | grep 'configure arguments:'`
       for opt in $options; do
           if [ `echo $opt | grep '.*-temp-path'` ]; then
               value=`echo $opt | cut -d "=" -f 2`
               if [ ! -d "$value" ]; then
                   # echo "creating" $value
                   mkdir -p $value && chown -R $user $value
               fi
           fi
       done
    }
     
    start() {
        [ -x $nginx ] || exit 5
        [ -f $NGINX_CONF_FILE ] || exit 6
        make_dirs
        echo -n $"Starting $prog: "
        daemon $nginx -c $NGINX_CONF_FILE
        retval=$?
        echo
        [ $retval -eq 0 ] && touch $lockfile
        return $retval
    }
     
    stop() {
        echo -n $"Stopping $prog: "
        killproc $prog -QUIT
        retval=$?
        echo
        [ $retval -eq 0 ] && rm -f $lockfile
        return $retval
    }
     
    restart() {
        configtest || return $?
        stop
        sleep 3 
        start
    }
     
    reload() {
        configtest || return $?
        echo -n $"Reloading $prog: "
        killproc $nginx -HUP
        RETVAL=$?
        echo
    }
     
    force_reload() {
        restart
    }
     
    configtest() {
      $nginx -t -c $NGINX_CONF_FILE
    }
     
    rh_status() {
        status $prog
    }
     
    rh_status_q() {
        rh_status >/dev/null 2>&1
    }
     
    case "$1" in
        start)
            rh_status_q && exit 0
            $1
            ;;
        stop)
            rh_status_q || exit 0
            $1
            ;;
        restart|configtest)
            $1
            ;;
        reload)
            rh_status_q || exit 7
            $1
            ;;
        force-reload)
            force_reload
            ;;
        status)
            rh_status
            ;;
        condrestart|try-restart)
            rh_status_q || exit 0
                ;;
        *)
            echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
            exit 2
    esac
    

    kafka脚本

    #!/bin/sh
    #chkconfig: 345 99 01
    #description: Kafka
    #File : Kafka
    #Description: Starts and stops the Kafka server
    #官方脚本
    
    source /etc/rc.d/init.d/functions
    
    export JAVA_HOME=/usr/java/jdk1.7.0_80
    export JRE_HOME=/usr/java/jdk1.7.0_80/jre
    KAFKA_HOME=/usr/local/kafka
    KAFKA_USER=root
    export LOG_DIR=/usr/local/kafka/logs
    
    [ -e /etc/sysconfig/kafka ] && . /etc/sysconfig/kafka
    
    #See how we were called.
    
    case "$1" in
    
      start)
        echo -n "Starting Kafka:"
        /sbin/runuser -s /bin/sh $KAFKA_USER -c "nohup $KAFKA_HOME/bin/kafka-server-start.sh $KAFKA_HOME/config/server.properties > $LOG_DIR/server.out 2> $LOG_DIR/server.err &"
        echo " done."
        exit 0
        ;;
    
      stop)
        echo -n "Stopping Kafka: "
        /sbin/runuser -s /bin/sh $KAFKA_USER  -c "ps -ef | grep kafka.Kafka | grep -v grep | awk '{print \$2}' | xargs kill"
        echo " done."
        exit 0
        ;;
      hardstop)
        echo -n "Stopping (hard) Kafka: "
        /sbin/runuser -s /bin/sh $KAFKA_USER  -c "ps -ef | grep kafka.Kafka | grep -v grep | awk '{print \$2}' | xargs kill -9"
        echo " done."
        exit 0
        ;;
    
      status)
        c_pid=`ps -ef | grep kafka.Kafka | grep -v grep | awk '{print $2}'`
        if [ "$c_pid" = "" ] ; then
          echo "Stopped"
          exit 3
        else
          echo "Running $c_pid"
          exit 0
        fi
        ;;
    
      restart)
        stop
        start
        ;;
    
      *)
        echo "Usage: kafka {start|stop|hardstop|status|restart}"
        exit 1
        ;;
    
    esac
    

    redis脚本

    #!/bin/sh
    #
    # Simple Redis init.d script conceived to work on Linux systems
    # as it does use of the /proc filesystem.
    # chkconfig:   2345 90 10
    # description:  Redis is a persistent key-value database
    
    
    REDISPORT=6379
    EXEC=/usr/local/redis/bin/redis-server
    CLIEXEC=/usr/local/redis/bin/redis-cli
    
    PIDFILE=/var/run/redis/redis.pid
    CONF="/usr/local/redis/etc/redis.conf"
    
    case "$1" in
        start)
            if [ -f $PIDFILE ]
            then
                    echo "$PIDFILE exists, process is already running or crashed"
            else
                    echo "Starting Redis server..."
                    $EXEC $CONF
            fi
            ;;
        stop)
            if [ ! -f $PIDFILE ]
            then
                    echo "$PIDFILE does not exist, process is not running"
            else
                    PID=$(cat $PIDFILE)
                    echo "Stopping ..."
                    $CLIEXEC -p $REDISPORT shutdown
                    while [ -x /proc/${PID} ]
                    do
                        echo "Waiting for Redis to shutdown ..."
                        sleep 1
                    done
                    echo "Redis stopped"
            fi
            ;;
        *)
            echo "Please use start or stop as first argument"
            ;;
    esac
    

    nodejs脚本

    #!/bin/bash
    # chkconfig: 2345 90 10
    # program:
    #   this program is a shell script to autostart nodejs.
    # Author: hhhhuanzi
    # Date: 2019-06-15          
    
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:/usr/local/nodejs/bin
    export PATH
    
    case $1 in
    "start")
            echo -n "Starting nodejs"
            cd /data/nodejs/&&
            forever start --minUptime 100 --spinSleepTime 100 -l nodejs.log -e err.log -a app.js&&
            echo " done."
            exit 0
            ;;
    
    "stop")
            echo -n "Stoping nodejs"
            cd /data/nodejs/&&
            forever stop --minUptime 100 --spinSleepTime 100 -l nodejs.log -e err.log -a app.js&&
            echo " done."
            exit 0
            ;;
    
    "restart")
            echo -n "Restarting nodejs"
            cd /data/nodejs/&&
            forever restart --minUptime 100 --spinSleepTime 100 -l nodejs.log -e err.log -a app.js&&
            echo "done."
            exit 0
            ;;
    *)
            echo "Usage:nodejs{start|stop|restart}"
            exit 1
            ;;
    esac
    

    相关文章

      网友评论

          本文标题:linux各服务启动脚本【nginx、tomcat、kafka、

          本文链接:https://www.haomeiwen.com/subject/rqpcdctx.html