如何编写Gradle启动脚本
我有一个Gradle应用程序,我使用如何编写Gradle启动脚本,gradle,startup,upstart,gradlew,Gradle,Startup,Upstart,Gradlew,我有一个Gradle应用程序,我使用/gradlew run启动它。这很好,但我正在尝试部署到AWS实例(Ubuntu12),我希望脚本在引导时执行。我尝试用上面的命令编写startup.sh文件,但没有成功。我还尝试将该命令添加到/etc/rc.local文件中,但这似乎也不起作用。有人能告诉我如何在启动时执行“./gradlew run”吗?谢谢 我编写了以下init脚本,用于在redhat发行版(centos/fedora等)的系统启动时启动gradle应用程序 您需要执行以下几个步骤来将
/gradlew run
启动它。这很好,但我正在尝试部署到AWS实例(Ubuntu12),我希望脚本在引导时执行。我尝试用上面的命令编写startup.sh文件,但没有成功。我还尝试将该命令添加到/etc/rc.local
文件中,但这似乎也不起作用。有人能告诉我如何在启动时执行“./gradlew run”吗?谢谢 我编写了以下init脚本,用于在redhat发行版(centos/fedora等)的系统启动时启动gradle应用程序
您需要执行以下几个步骤来将其结合在一起:
gradle distZip
将gradle应用程序部署到目标服务器上/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<maxShutdownTime*5; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ] ; then
rm -f $pidfile
return 0
fi
sleep 0.2
done
# hard kill now...
kill -s KILL $pid || return 1
# check if process dead, sleep 0.2s otherwise
for ((i=0; i<maxShutdownTime*5; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ] ; then
rm -f $pidfile
return 0
fi
sleep 0.2
done
return 1
}
stop() {
getServicePID
if [ $? -ne 0 ]; then echo -n "$serviceName is not running"; RETVAL=0; echo ""; return 0; fi
pid="$(<$pidfile)"
echo -n "Stopping $serviceName "
stopService
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
echo "stopped PID=$pid"
RETVAL=0
return 0
}
restart() {
stop
start
}
checkServiceStatus() {
echo -n "Checking for $serviceName: "
if getServicePID; then
echo "running PID=$pid"
RETVAL=0
else
echo "stopped"
RETVAL=3
fi
return 0;
}
####### START OF MAIN SCRIPT
RETVAL=0
case "$1" in
start)
$1
;;
stop)
$1
;;
restart)
$1
;;
status)
checkServiceStatus
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
esac
exit $RETVAL
#/bin/bash
#
#chkconfig:34580 20
#描述:gradle创建的java应用程序启动的启动和停止脚本
#
#这是一个通用文件,可由gradle的任何发行版(“gradle distZip”)使用。
#将此文件链接到要运行的进程的名称。
#例如。
#ln-s/path/to/gradle init start-stop/etc/init.d/ivy-jetty
#
#它需要一个conf文件/etc/NAME.conf,例如/etc/ivy-jetty.conf
#否则它将退出。
#
#配置文件条目:
# ---------------------------
#用户名=进程所有者
#prog=“/path/to/gradle startscript-a任意-e额外参数”
#serviceName=SomeShortNameForService
#javaClass=package.for.javaClass
. /etc/rc.d/init.d/functions
BASENAME=$(BASENAME$0)
最大停机时间=15
CONF=/etc/${BASENAME}.CONF
pidfile=/var/run/$BASENAME.pid
如果[!-f$CONF];然后
echo“找不到配置文件:$CONF”
出口1
fi
#######源配置文件
来源$CONF
checkProcessIsRunning(){
本地pid=“$1”
如果[-z“$pid”-o“$pid”==”];则返回1;fi
如果[!-e/proc/$pid];则返回1;fi
返回0
}
checkProcessOutserService(){
本地pid=“$1”
如果[“$(ps-p$pid——无头-o通信)”!=“java”];则返回1;fi
grep-q--binary-F“$javaClass”/proc/$pid/cmdline
如果[$?-ne 0];则返回1;fi
返回0
}
getServicePID(){
如果[!-f$pidfile];则返回1;fi
pid=“$(/dev/null 2>&1&echo\$!”
sudo-u$username-H$SHELL-c“$cmd”>$pidfile
睡眠0.2
pid=“$”(我编写了以下初始化脚本,用于在redhat发行版(centos/fedora等)的系统启动时启动gradle应用程序
您需要执行以下几个步骤来将其结合在一起:
使用gradle distZip
将gradle应用程序部署到目标服务器上
创建一个配置文件/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<maxShutdownTime*5; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ] ; then
rm -f $pidfile
return 0
fi
sleep 0.2
done
# hard kill now...
kill -s KILL $pid || return 1
# check if process dead, sleep 0.2s otherwise
for ((i=0; i<maxShutdownTime*5; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ] ; then
rm -f $pidfile
return 0
fi
sleep 0.2
done
return 1
}
stop() {
getServicePID
if [ $? -ne 0 ]; then echo -n "$serviceName is not running"; RETVAL=0; echo ""; return 0; fi
pid="$(<$pidfile)"
echo -n "Stopping $serviceName "
stopService
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
echo "stopped PID=$pid"
RETVAL=0
return 0
}
restart() {
stop
start
}
checkServiceStatus() {
echo -n "Checking for $serviceName: "
if getServicePID; then
echo "running PID=$pid"
RETVAL=0
else
echo "stopped"
RETVAL=3
fi
return 0;
}
####### START OF MAIN SCRIPT
RETVAL=0
case "$1" in
start)
$1
;;
stop)
$1
;;
restart)
$1
;;
status)
checkServiceStatus
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
esac
exit $RETVAL
!/bin/bash
#
#chkconfig:34580 20
#描述:gradle创建的java应用程序启动的启动和停止脚本
#
#这是一个通用文件,可由gradle的任何发行版(“gradle distZip”)使用。
#将此文件链接到要运行的进程的名称。
#例如。
#ln-s/path/to/gradle init start-stop/etc/init.d/ivy-jetty
#
#它需要一个conf文件/etc/NAME.conf,例如/etc/ivy-jetty.conf
#否则它将退出。
#
#配置文件条目:
# ---------------------------
#用户名=进程所有者
#prog=“/path/to/gradle startscript-a任意-e额外参数”
#serviceName=SomeShortNameForService
#javaClass=package.for.javaClass
/etc/rc.d/init.d/functions
BASENAME=$(BASENAME$0)
最大停机时间=15
CONF=/etc/${BASENAME}.CONF
pidfile=/var/run/$BASENAME.pid
如果[!-f$CONF];则
echo“找不到配置文件:$CONF”
出口1
fi
#######源配置文件
来源$CONF
checkProcessIsRunning(){
本地pid=“$1”
如果[-z“$pid”-o“$pid”==”];则返回1;fi
如果[!-e/proc/$pid];则返回1;fi
返回0
}
checkProcessOutserService(){
本地pid=“$1”
如果[“$(ps-p$pid——无头-o通信)”!=“java”];则返回1;fi
grep-q--binary-F“$javaClass”/proc/$pid/cmdline
如果[$?-ne 0];则返回1;fi
返回0
}
getServicePID(){
如果[!-f$pidfile];则返回1;fi
pid=“$(/dev/null 2>&1&echo\$!”
sudo-u$username-H$SHELL-c“$cmd”>$pidfile
睡眠0.2
pid=”$(非常彻底的回答,谢谢!我最后执行了一个简单的守护进程运行&
,但你的回答正是我需要的。我会回去正确设置它。@MichaelDJohnson很高兴我能帮上忙。非常彻底的回答,谢谢!我最后执行了一个简单的守护进程运行&
,但是你的answer正是我所需要的。我会回去正确设置它。@MichaelDJohnson很高兴我能帮上忙。