sh 支持在内部通过命令启动/停止pid文件的应用程序的公共init文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 支持在内部通过命令启动/停止pid文件的应用程序的公共init文件相关的知识,希望对你有一定的参考价值。

#!/bin/sh
### BEGIN INIT INFO
# Provides:          $app_name
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: lightweight 
# Description:       app is a lightweight 
#                    for embedded devices and low end boxes.
#
### END INIT INFO

# Author: Common Contribution

# PATH should only include /usr/ if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
PY=python
DESC=initfile       # Introduce a short description here
NAME=initfile      # Introduce the short server's name here
DAEMON="/path/to/initfile.py"    # Introduce the server's location here
SCRIPTNAME=/etc/init.d/$NAME
LOGDIR=/var/log
LOGPATH=$LOGDIR/$name.log
CONFFILE=/path/to/conf

if [ ! -f $LOGPATH ]; then
   sudo touch $LOGPATH
fi

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

# $DAEMON app should have good support for pid file 
#### and starting/stopping command
#### so there's no need to create pid file
#### or the need for start-stop-daemon

#
# Function that starts the daemon/service
#
do_start()
{
    # -u option disables buffters for py interpreter.
    $PY -u $DAEMON -c $CONFFILE --log-file $LOGPATH -d start >> $LOGPATH 2>&1
    echo "" >> $LOGPATH
}


#
# Function that stops the daemon/service
#
do_stop()
{    
    $PY -u $DAEMON -c $CONFFILE -d stop >> $LOGPATH 2>&1
    echo "" >> $LOGPATH
}

case "$1" in
    start)
        #[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME"
        log_daemon_msg "Starting $DESC " "$NAME"
        do_start
        case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
    ;;
    stop)
        #[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
    ;;
    status)
        status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;
    restart|force-reload)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
            0|1)
                do_start
                case "$?" in
                    0) log_end_msg 0 ;;
                    1) log_end_msg 1 ;; # Old process is still running
                    *) log_end_msg 1 ;; # Failed to start
                esac
            ;;
            *)
                # Failed to stop
                log_end_msg 1
            ;;
        esac
    ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
        exit 3
    ;;
esac

以上是关于sh 支持在内部通过命令启动/停止pid文件的应用程序的公共init文件的主要内容,如果未能解决你的问题,请参考以下文章

linux下如何屏蔽端口

为啥包含 300 张图像的应用程序停止工作?

通过Shell开发企业级标准服务启动脚本案例(MySQL)

tomcat使用stop.sh脚本无法关闭

部署项目常用命令

linux中启动Nginx