2

プロセスの 1 つが実行されているかどうかを 10 分ごとに確認しようとしています。実行されていない場合は、そのプロセスを再起動します。システムの起動時にこのスクリプトを自動的に開始したいので、 Linux のサービスを探します。

だからここに私がしたことがあります:

  • Bash スクリプトをサービスとして作成しました。
  • シェルスクリプトのstartメソッドで、無限 while ループ内で、一時ファイルが存在するかどうかを確認します。利用可能な場合は私のロジックを実行し、そうでない場合はループを中断します。
  • メソッドで、stop一時ファイルを削除します。
  • 次に、update-rc.d を使用して、このスクリプトをシステムの起動時に追加しました。

1 つのことを除いて、すべてが正常に機能していました。実行すると./myservice start、端末がハングアップします (無限ループを実行しているため) が、実行するctrl-zとスクリプトが強制終了され、タスクが実行されません。このスクリプトを端末から開始して正常に実行するにはどうすればよいですか? (のように、言う./etc/init.d/mysql start)。バックグラウンドでプロセスを実行して戻る場合があります。

私の Bash スクリプトを以下に示します。

#!/bin/bash

# Start the service
start() {

    #Process name that need to be monitored
    process_name="mysqld"

    #Restart command for process
    restart_process_command="service mysql start"

    #path to pgrep command
    PGREP="/usr/bin/pgrep"

    #Initially, on startup do create a testfile to indicate that the process
    #need to be monitored. If you dont want the process to be monitored, then
    #delete this file or stop this service
        touch /tmp/testfile

        while true;
        do

        if [ ! -f /tmp/testfile ]; then
             break
        fi

        $PGREP ${process_name}

        if [ $? -ne 0 ] # if <process> not running
        then
        # restart <process>
        $restart_process_command
        fi

        #Change the time for monitoring process here (in secs)
        sleep 1000

    done
}

stop() {
       echo "Stopping the service"
       rm -rf /tmp/testfile
}
### main logic ###
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac
exit 0
4

3 に答える 3

4

バックグラウンドで関数を実行します。言う:

start)
      start &

言う代わりに:

start)
      start
于 2013-11-13T12:50:55.707 に答える
1

サイクル自体をのスクリプトに切り離し、非自己デーモン化アプリケーションのディストリビューションで推奨される方法に従って、スタートアップ スクリプトから後者のスクリプトを実行する必要があります。

現在の起動スクリプトは、少なくともほとんどの従来の起動では、ターミナルから実行しなくても、システムの起動をハングアップさせる可能性があることに注意してください (systemd や、複数のスクリプトを並行して起動できるその他の現代的な起動については言いませんが、間違いなく起動が完了していないと考えます)。ただし、たとえば ssh 経由でログインした場合は、スクリプトの前に sshd が開始されるため、見逃す可能性があります。

于 2013-11-13T12:48:43.623 に答える
1

cron私の意見では、スタンドアロンの init スクリプトを作成するよりも、この種のスクリプトをスケジューラでスケジューリングする価値があります。

cat > /usr/local/bin/watch-mysql.sh <<-"__SEOF__"
    #!/bin/sh
    set -e

    # if this file exists monitoring is stopped
    if [ -f /var/run/dont-watch-mysql.lck ]; then
      exit 0
    fi

    process_name="mysqld"        
    restart_process_command="service mysql restart"
    PGREP="/usr/bin/pgrep"

    $PGREP ${process_name}

    if [ $? -ne 0 ] # if <process> not running
    then
        # restart <process>
        $restart_process_command
    fi
__SEOF__

# make the script executable
chmod u+x /usr/local/bin/watch-mysql.sh

# schedule the script to run every 10 minutes
(crontab -l ; echo "*/10 * * * * /usr/local/bin/watch-mysql.sh") | uniq - | crontab -

ファイルを作成しただけ/var/run/watch-mysql.lckでは監視されません。

于 2013-11-13T13:03:05.303 に答える