我有一个我启动时使用的Gradle应用程序./gradlew run
.这工作正常,但我正在尝试部署到AWS实例(Ubuntu 12),我希望脚本在启动时执行.我尝试使用上面的命令编写一个startup.sh文件,但没有骰子.我也尝试将命令添加到/etc/rc.local
文件中,但这似乎也不起作用.有人可以告诉我如何在启动时执行`./gradlew run'吗?谢谢!
我编写了以下init脚本,用于在系统启动时为redhat发行版(centos/fedora等)启动gradle应用程序.
您需要执行几个步骤将它们组合在一起:
将gradle应用程序部署gradle distZip
到目标服务器上
创建配置文件/etc/my-service.conf
将init脚本(见下文)链接到/etc/init.d/my-service中的服务名称
示例配置文件 /etc/my-service.conf
username=someunixuser serviceName=MyStandaloneServer prog="/path/to/bin/MyStandaloneServer -a any -p params -y you -w want" javaClass="some.java.MyStandaloneServer"
请注意prog行中distZip的应用程序路径.
然后,将init脚本链接到您希望它运行的实际服务,例如
ln -s /path/to/gradle-init-start-stop /etc/init.d/my-service
完成此操作后,您可以使用chkconfig以通常的方式添加服务(默认为3/4/5)
这是脚本 gradle-init-start-stop
#!/bin/bash # # chkconfig: 345 80 20 # description: Start and stop script for gradle created java application startup # # This is a generic file that can be used by any distribution from gradle ("gradle distZip"). # Link this file to the name of the process you want to run. # e.g. # ln -s /path/to/gradle-init-start-stop /etc/init.d/ivy-jetty # # it requires a conf file /etc/NAME.conf, e.g. /etc/ivy-jetty.conf # otherwise it will quit. # # CONFIGURATION FILE ENTRIES: # --------------------------- # username=process-owner # prog="/path/to/gradle-startscript -a any -e extra parameters" # serviceName=SomeShortNameForService # javaClass=package.for.JavaClass . /etc/rc.d/init.d/functions BASENAME=$(basename $0) maxShutdownTime=15 CONF=/etc/${BASENAME}.conf pidfile=/var/run/$BASENAME.pid if [ ! -f $CONF ] ; then echo "Could not find configuration file: $CONF" exit 1 fi ####### SOURCE CONFIGURATION FILE source $CONF checkProcessIsRunning() { local pid="$1" if [ -z "$pid" -o "$pid" == " " ]; then return 1; fi if [ ! -e /proc/$pid ]; then return 1; fi return 0 } checkProcessIsOurService() { local pid="$1" if [ "$(ps -p $pid --no-headers -o comm)" != "java" ]; then return 1; fi grep -q --binary -F "$javaClass" /proc/$pid/cmdline if [ $? -ne 0 ]; then return 1; fi return 0 } getServicePID() { if [ ! -f $pidfile ]; then return 1; fi pid="$(<$pidfile)" checkProcessIsRunning $pid || return 1 checkProcessIsOurService $pid || return 1 return 0 } startService() { cmd="nohup $prog >/dev/null 2>&1 & echo \$!" sudo -u $username -H $SHELL -c "$cmd" > $pidfile sleep 0.2 pid="$(<$pidfile)" if checkProcessIsRunning $pid; then return 0 else return 1 fi } start() { getServicePID if [ $? -eq 0 ]; then echo -n "$serviceName is already running"; RETVAL=0; echo ""; return 0; fi echo -n "Starting $serviceName: " startService if [ $? -ne 0 ] ; then echo "failed" return 1 else echo "started" return 0 fi } stopService() { # soft kill first... kill $pid || return 1 # check if process dead, sleep 0.2s otherwise for ((i=0; i