如何将 Ubuntu 初始化脚本转换为 Amazon Linux AMI 初始化脚本?

如何将 Ubuntu 初始化脚本转换为 Amazon Linux AMI 初始化脚本?

我有一个基于 Ubuntu 的 Amazon EC2 服务器,我在其中运行基于 java 的应用程序。为此,我编写了一个运行完美的初始化脚本,如下所示

 #! /bin/sh
# set the environment variables that are used
PATH=/sbin:/usr/sbin:/bin:/usr/bin
JAVA_HOME=/usr/lib/jvm/java-6-sun/jre
JAVA=$JAVA_HOME/bin/java
DESC="My Application"
NAME="myserver"
DAEMON=$JAVA
DAEMON_HOME="/home/ganesh/MyServer/"
JAR=$DAEMON_HOME/MyServer.jar
DAEMON_ARGS="-Xms512m -Xmx4112m -jar $JAR"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/myserver
#the user that will run the script
USER=ganesh

# 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

#
# Function that starts the daemon/service
#
do_start()
{
        start-stop-daemon -N -10 -b --start -d $DAEMON_HOME --quiet --chuid $USER -m -p $PIDFILE --exec $DAEMON -- $DAEMON_ARGS \
                || return 2


        #Test to see if the engine has started
        start-stop-daemon -b --test --start -d $DAEMON_HOME --quiet --chuid $USER -m -p $PIDFILE --exec $DAEMON -- $DAEMON_ARGS >/dev/null 2>&1

    case "$?" in
        0) echo "[ FAIL ] App Engine has not started";;
        1) echo "[ OK ] App Engine has started";;
    esac                               
}

#
# Function that stops the daemon/service
#
do_stop()
{
        start-stop-daemon --stop --retry 120 --oknodo --pidfile $PIDFILE
        RETVAL="$?"
        rm -f $PIDFILE
        return "$RETVAL"
}

case "$1" in
  start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        echo "Starting $DESC" "$NAME"

        do_start
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1
                                echo "[ FAIL ]";;              
        esac
        ;;

  stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        echo "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        echo "[ OK ]"
        ;;
  restart)
        #
        # If the "reload" option is implemented then remove the
        # 'force-reload' alias
        #
        log_daemon_msg "Restarting $DESC" "$NAME"
        echo "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|restart}" >&2
        exit 3
        ;;
esac

:

现在我已将服务器从 Ubuntu 迁移到 Amazon Linux AMI。所以我将上面的初始化脚本更改如下

#!/bin/bash

# Source function library.
. /etc/init.d/functions

RETVAL=0
prog="myserver"
LOCKFILE=/var/lock/subsys/$prog
PIDFILE=/var/run/$prog.pid
PATH=/sbin:/usr/sbin:/bin:/usr/bin
JAVA=/usr/bin/java
DESC="MY Application"
DAEMON=$JAVA
DAEMON_HOME="/home/ganesh/MyServer"
JAR=$DAEMON_HOME/MyServer.jar
DAEMON_ARGS="-Xms512m -Xmx4112m -jar $JAR"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/myserver
#the user that will run the script
USER=root

echo "All value sets"

start() {
        echo "IN start"
        echo -n "Starting $prog: "
        daemon --user $USER --chdir $DAEMON_HOME --pidfile $PIDFILE $DAEMON -- $DAEMON_ARGS
        RETVAL=$?
        [ $RETVAL -eq 0 ] && touch $LOCKFILE
        echo
        return $RETVAL
}

stop() {
        echo -n "Shutting down $prog: "
        killproc --pidfile
        RETVAL=$?
        [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
        echo
        return $RETVAL
}

status() {
        echo -n "Checking $prog status: "
        RETVAL=$?
        return $RETVAL
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage1: $prog {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $RETVAL

我在替换启动和停止函数中的以下主线时遇到问题

start-stop-daemon -N -10 -b --start -d $DAEMON_HOME --quiet --chuid
$USER -m -p $PIDFILE --exec $DAEMON -- $DAEMON_ARGS \
              || return 2

start-stop-daemon --stop --retry 120 --oknodo --pidfile $PIDFILE

Amazon Linux 将“start-stop-daemon”替换为“daemon”,并在手册页的帮助下,我对上面的行进行了如下更改

daemon --user $USER --chdir $DAEMON_HOME --pidfile $PIDFILE $DAEMON -- $DAEMON_ARGS

killproc --pidfile

但它给出以下错误。

启动 myserver: ./myserver: 用法: 守护进程 [+/-nicelevel] {program}

有人会指导我为 Amazon Linux AMI 编写正确的初始化脚本吗?

编辑:

在启动函数中,我做了以下更改,现在此脚本启动守护进程,但无法创建 pidfile,因此无法停止守护进程

cd $DAEMON_HOME
daemon --user $USER --pidfile $PIDFILE $DAEMON $DAEMON_ARGS >/dev/null 2>&1 & 

那么有人可以建议我它有什么问题以及我需要做什么才能自动创建 pid 文件吗?

答案1

我将脚本更新为以下代码。虽然它无法创建 pidfile,但它可以按我想要的方式工作。如果有人有更好的脚本,请发布您的答案。

#!/bin/bash

# chkconfig: 2345 95 05
# description: MY Application

# Source function library.
. /etc/init.d/functions

RETVAL=0
prog="myserver"
PIDFILE=/var/run/$prog.pid
LOCKFILE=/var/lock/subsys/$prog
PATH=/sbin:/usr/sbin:/bin:/usr/bin
JAVA=/usr/bin/java
DESC="MY Application"
DAEMON=$JAVA
DAEMON_HOME="/home/ganesh/MyServer/"
JAR=$DAEMON_HOME/MyServer.jar
DAEMON_ARGS="-Xms512m -Xmx4112m -jar $JAR"
SCRIPTNAME=/etc/init.d/myserver
#the user that will run the script
USER=root

#echo "All value sets"

start() {
    if [ -f $LOCKFILE ];
    then
        echo "$DESC is already running. Exiting."
        exit 1
    else
        echo -n "Starting $prog:"
        cd $DAEMON_HOME
        daemon --user $USER  --pidfile $PIDFILE $DAEMON $DAEMON_ARGS >/dev/null 2>&1 &
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch $LOCKFILE
    fi

    return $RETVAL
}

stop() {
        echo -n "Shutting down "$prog:
        kill -9  `ps -ef | grep "$JAR" | grep -v grep | awk '{ print $2 }'`        
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
        return $RETVAL
}

check_status() {
        #echo -n "Checking $prog status: "
        status $prog
        RETVAL=$?
        return $RETVAL
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        check_status
        ;;
    restart)
        stop
        start
        ;;
    *)

        echo "Usage: $prog {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $RETVAL

编辑:

上述脚本的另一个版本,我正在创建 pid 文件并使用它来获取状态并停止应用程序。我认为下面的脚本比上面的更可靠。

#!/bin/bash

# chkconfig: 2345 95 05
# description: MY Application

# Source function library.
. /etc/init.d/functions

RETVAL=0
prog="myserver"
PIDFILE=/var/run/$prog.pid
LOCKFILE=/var/lock/subsys/$prog
PATH=/sbin:/usr/sbin:/bin:/usr/bin
JAVA=/usr/bin/java
DESC="MY Application"
DAEMON=$JAVA
DAEMON_HOME="/home/ganesh/MyServer/"
JAR=$DAEMON_HOME/MyServer.jar
DAEMON_ARGS="-Xms512m -Xmx4112m -jar $JAR"
SCRIPTNAME=/etc/init.d/myserver
#the user that will run the script
USER=root

#echo "All value sets"

start() {
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        if [ -z "`pgrep $PID`" ] && [ "$PID" != "`ps aux|grep -vE 'grep|runuser|bash'|grep -w "$JAR"|awk '{print $2}'`" ]; then
            printf "%s\n" "Process dead but pidfile exists"
        else
            printf "$prog is already running!\n"
        fi
    else
        printf "%-50s" "Starting $prog ..."
        cd $DAEMON_HOME
        daemon --user $USER $DAEMON $DAEMON_ARGS >/dev/null 2>&1 &
        sleep 5
        PID=`ps aux|grep -vE 'grep|runuser|bash'|grep -w "$JAR"|awk '{print $2}'`
        if [ -z "$PID" ]; then
            printf "[ \e[31mFAIL\033[0m ]\n"
        else
            echo $PID > $PIDFILE
            printf "[ \e[32mOK\033[0m ]\n"
        fi
    fi
}

stop() {
    printf "%-50s" "Shutting down $prog:"
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        kill -HUP $PID 2>/dev/null
        printf "[ \e[32mOK\033[0m ]\n"
        rm -f $PIDFILE
    else
        printf "[ \e[31mFAIL\033[0m ]\n" 
    fi
}

check_status() {
    printf "%-50s" "Checking $prog ..."
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        if [ -z "`pgrep $PID`" ] && [ "$PID" != "`ps aux|grep -vE 'grep|runuser|bash'|grep -w "$JAR"|awk '{print $2}'`" ]; then
            printf "%s\n" "Process dead but pidfile exists"
        else
            printf "[ \e[32mRUNNING\033[0m ]\n"
        fi
    else
        printf "[ \e[31mSTOPPED\033[0m ]\n"
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        check_status
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage: $prog {start|stop|status|restart}"
        exit 1
        ;;
esac
exit 1

答案2

我没有重写脚本,而是选择安装start-stop-daemon.

# Follow instructions here to make the RPM available to yum
# https://packagecloud.io/willgarcia/start-stop-daemon/install
# and then you can run:
yum install start-stop-daemon

来源可以在这里找到:https://github.com/willgarcia/start-stop-daemon

相关内容