如何将“play start”作为 Linux 服务运行

如何将“play start”作为 Linux 服务运行

我想从源代码部署一个 play 框架 Web 应用程序,并运行play start以启动该应用程序。

我写了一个启动脚本,在服务启动时/etc/init.d/执行,但服务启动命令没有返回。daemon play start

我想这是因为play start正在等我输入Ctrl+ Dnohup可以修复它,但是对于nohup,我必须运行kill -9 xxx才能停止应用程序,这不是我喜欢的。

从源代码运行 play 框架应用程序作为 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

相关内容