「play start」をLinuxサービスとして実行する方法

「play start」をLinuxサービスとして実行する方法

play startソースコードからプレイフレームワークWebアプリケーションをデプロイして実行してアプリケーションを起動したいと思います。

/etc/init.d/サービスの起動時に実行される起動スクリプトを作成しましたが、サービスの起動コマンドは返されません。daemon play start

+をplay start入力するのを待っているからです。修正することはできますが、そうするにはアプリケーションを停止するために実行する必要がありますが、それは気に入らません。CtrlDnohupnohupkill -9 xxx

ソースコードでプレイフレームワークアプリケーションをLinux起動サービスとして実行する最良の方法は何ですか?

ベストアンサー1

これはinit.d次の簡単なスクリプトです。

  • start:アプリケーションがまだ起動していない場合にのみ、バックグラウンドでアプリケーションを再コンパイルし(必要な場合)起動します。
  • stop: アプリケーションの終了

コードのコメントを注意深く読んでください。

#!/bin/sh
# /etc/init.d/playapp

# Play project directory is in /var/play/playapp/www, not directly in SDIR
SDIR="/var/play/playapp"

# The following part always gets executed.
echo "PLAYAPP Service"

# The following part carries out specific functions depending on arguments.
case "$1" in
  start)
    echo " * Starting PLAYAPP Service"
    
    if [ -f ${SDIR}/www/target/universal/stage/RUNNING_PID ]
    then        
        PID=$(cat ${SDIR}www/target/universal/stage/RUNNING_PID)
        
        if ps -p $PID > /dev/null
        then
            echo "   service already running ($PID)"
            exit 1
        fi
    fi
    
    cd ${SDIR}/www
    
    # REPLACE "PROJECT_NAME" with your project name
    
    if [ ! -f ${SDIR}/www/target/universal/stage/bin/PROJECT_NAME ]
    then    
        echo "   recompiling..."
        
        # REPLACE path to your play command
        /var/play-install/play/play clean compile stage
    fi

    echo "   starting..."
    nohup ./target/universal/stage/bin/PROJECT_NAME -Dhttp.port=9900 -Dconfig.file=/var/play/playapp/www/conf/application-prod.conf > application.log 2>&1&
    ;;
  stop)
    echo " * Stopping PLAYAPP Service"
    
    if [ ! -f ${SDIR}/www/target/universal/stage/RUNNING_PID ]
    then
        echo "   nothing to stop"
        exit 1;
    fi
    
    kill -TERM $(cat ${SDIR}/www/target/universal/stage/RUNNING_PID)    
    ;;
  *)
    echo "Usage: /etc/init.d/playapp {start|stop}"
    exit 1
    ;;
esac

exit 0

おすすめ記事