运行 一个 Jar 的启动脚本
Startup Script to Run a Jar
我有一个胖 jar 文件,我想通过 运行 将其作为服务进行控制。我不是 shell 脚本专家,但从这个博客 post:
中获得了一些灵感
http://vertx.io/blog/vert-x-3-init-d-script/
然后我根据自己的需要对其进行了如下修改:
#!/bin/bash
###
# chkconfig: 345 20 80
# description: Vert.x application service script
# processname: java
#
# Installation (CentOS):
# copy file to /etc/init.d
# chmod +x /etc/init.d/my-vertx-application
# chkconfig --add /etc/init.d/my-vertx-application
# chkconfig my-vertx-application on
#
# Installation (Ubuntu):
# copy file to /etc/init.d
# chmod +x /etc/init.d/my-vertx-application
# update-rc.d my-vertx-application defaults
#
#
# Usage: (as root)
# service my-vertx-application start
# service my-vertx-application stop
# service my-vertx-application status
#
###
# The directory in which your application is installed
APPLICATION_DIR="/Users/joe/Desktop/temp/tsdb-kafka-consumer"
# The fat jar containing your application
APPLICATION_JAR="tsdb-kafka-consumer-0.1.0-SNAPAHOT.jar"
# The application argument such as -Dfoo=bar ...
APPLICATION_ARGS="-Dlogback.configurationFile=prod-logger.xml -Dconfig.resource=application.integration.conf"
# The Java command to use to launch the application (must be java 8+)
#JAVA=/opt/java/java/bin/java
# ***********************************************
LOG_FILE="${APPLICATION_DIR}"
RUNNING_PID="${APPLICATION_DIR}"/RUNNING_PID
# ***********************************************
# colors
red='\e[0;31m'
green='\e[0;32m'
yellow='\e[0;33m'
reset='\e[0m'
echoRed() { echo -e "${red}${reset}"; }
echoGreen() { echo -e "${green}${reset}"; }
echoYellow() { echo -e "${yellow}${reset}"; }
# Check whether the application is running.
# The check is pretty simple: open a running pid file and check that the process
# is alive.
isrunning() {
# Check for running app
if [ -f "$RUNNING_PID" ]; then
proc=$(cat $RUNNING_PID);
if /bin/ps --pid $proc 1>&2 >/dev/null;
then
return 0
fi
fi
return 1
}
start() {
if isrunning; then
echoYellow "The Vert.x application is already running"
return 0
fi
pushd $APPLICATION_DIR > /dev/null
nohup java $APPLICATION_ARGS -jar $APPLICATION_DIR/$APPLICATION_JAR > $LOG_FILE 2>&1 &
echo $! > ${RUNNING_PID}
popd > /dev/null
if isrunning; then
echoGreen "Kafka Consumer Application started"
exit 0
else
echoRed "Kafka Consumer Application has not started - check log"
exit 3
fi
}
restart() {
echo "Restarting Kafka Consumer Application"
stop
start
}
stop() {
echoYellow "Stopping Kafka Consumer Application"
if isrunning; then
kill `cat $RUNNING_PID`
rm $RUNNING_PID
fi
}
status() {
if isrunning; then
echoGreen "Kafka Consumer Application is running"
else
echoRed "Kafka Consumer Application is either stopped or inaccessible"
fi
}
case "" in
start)
start
;;
status)
status
exit 0
;;
stop)
if isrunning; then
stop
exit 0
else
echoRed "Application not running"
exit 3
fi
;;
restart)
stop
start
;;
*)
echo "Usage: [=11=] {status|start|stop|restart}"
exit 1
esac
当我尝试 运行 这个脚本时,我收到以下错误:
Joes-MacBook-Pro:script joe$ sh ./run.sh start
/bin/ps: illegal option -- -
usage: ps [-AaCcEefhjlMmrSTvwXx] [-O fmt | -o fmt] [-G gid[,gid...]]
[-g grp[,grp...]] [-u [uid,uid...]]
[-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]
ps [-L]
./run.sh: line 73: /Users/joe/Desktop/temp/tsdb-kafka-consumer: Is a directory
/bin/ps: illegal option -- -
usage: ps [-AaCcEefhjlMmrSTvwXx] [-O fmt | -o fmt] [-G gid[,gid...]]
[-g grp[,grp...]] [-u [uid,uid...]]
[-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]
ps [-L]
-e \e[0;31mKafka Consumer Application has not started - check log\e[0m
这是什么问题?有什么建议吗?
--pid
参数在您的 OSX 上无效。您应该将 isrunning()
上的 ps --pid
更改为 ps -p
。这将适用于 Linux 和 OSX。
我有一个胖 jar 文件,我想通过 运行 将其作为服务进行控制。我不是 shell 脚本专家,但从这个博客 post:
中获得了一些灵感http://vertx.io/blog/vert-x-3-init-d-script/
然后我根据自己的需要对其进行了如下修改:
#!/bin/bash
###
# chkconfig: 345 20 80
# description: Vert.x application service script
# processname: java
#
# Installation (CentOS):
# copy file to /etc/init.d
# chmod +x /etc/init.d/my-vertx-application
# chkconfig --add /etc/init.d/my-vertx-application
# chkconfig my-vertx-application on
#
# Installation (Ubuntu):
# copy file to /etc/init.d
# chmod +x /etc/init.d/my-vertx-application
# update-rc.d my-vertx-application defaults
#
#
# Usage: (as root)
# service my-vertx-application start
# service my-vertx-application stop
# service my-vertx-application status
#
###
# The directory in which your application is installed
APPLICATION_DIR="/Users/joe/Desktop/temp/tsdb-kafka-consumer"
# The fat jar containing your application
APPLICATION_JAR="tsdb-kafka-consumer-0.1.0-SNAPAHOT.jar"
# The application argument such as -Dfoo=bar ...
APPLICATION_ARGS="-Dlogback.configurationFile=prod-logger.xml -Dconfig.resource=application.integration.conf"
# The Java command to use to launch the application (must be java 8+)
#JAVA=/opt/java/java/bin/java
# ***********************************************
LOG_FILE="${APPLICATION_DIR}"
RUNNING_PID="${APPLICATION_DIR}"/RUNNING_PID
# ***********************************************
# colors
red='\e[0;31m'
green='\e[0;32m'
yellow='\e[0;33m'
reset='\e[0m'
echoRed() { echo -e "${red}${reset}"; }
echoGreen() { echo -e "${green}${reset}"; }
echoYellow() { echo -e "${yellow}${reset}"; }
# Check whether the application is running.
# The check is pretty simple: open a running pid file and check that the process
# is alive.
isrunning() {
# Check for running app
if [ -f "$RUNNING_PID" ]; then
proc=$(cat $RUNNING_PID);
if /bin/ps --pid $proc 1>&2 >/dev/null;
then
return 0
fi
fi
return 1
}
start() {
if isrunning; then
echoYellow "The Vert.x application is already running"
return 0
fi
pushd $APPLICATION_DIR > /dev/null
nohup java $APPLICATION_ARGS -jar $APPLICATION_DIR/$APPLICATION_JAR > $LOG_FILE 2>&1 &
echo $! > ${RUNNING_PID}
popd > /dev/null
if isrunning; then
echoGreen "Kafka Consumer Application started"
exit 0
else
echoRed "Kafka Consumer Application has not started - check log"
exit 3
fi
}
restart() {
echo "Restarting Kafka Consumer Application"
stop
start
}
stop() {
echoYellow "Stopping Kafka Consumer Application"
if isrunning; then
kill `cat $RUNNING_PID`
rm $RUNNING_PID
fi
}
status() {
if isrunning; then
echoGreen "Kafka Consumer Application is running"
else
echoRed "Kafka Consumer Application is either stopped or inaccessible"
fi
}
case "" in
start)
start
;;
status)
status
exit 0
;;
stop)
if isrunning; then
stop
exit 0
else
echoRed "Application not running"
exit 3
fi
;;
restart)
stop
start
;;
*)
echo "Usage: [=11=] {status|start|stop|restart}"
exit 1
esac
当我尝试 运行 这个脚本时,我收到以下错误:
Joes-MacBook-Pro:script joe$ sh ./run.sh start
/bin/ps: illegal option -- -
usage: ps [-AaCcEefhjlMmrSTvwXx] [-O fmt | -o fmt] [-G gid[,gid...]]
[-g grp[,grp...]] [-u [uid,uid...]]
[-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]
ps [-L]
./run.sh: line 73: /Users/joe/Desktop/temp/tsdb-kafka-consumer: Is a directory
/bin/ps: illegal option -- -
usage: ps [-AaCcEefhjlMmrSTvwXx] [-O fmt | -o fmt] [-G gid[,gid...]]
[-g grp[,grp...]] [-u [uid,uid...]]
[-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]
ps [-L]
-e \e[0;31mKafka Consumer Application has not started - check log\e[0m
这是什么问题?有什么建议吗?
--pid
参数在您的 OSX 上无效。您应该将 isrunning()
上的 ps --pid
更改为 ps -p
。这将适用于 Linux 和 OSX。