我一直在尝试让 Java 应用程序作为服务运行。我用过这个spring框架指南将来自不同来源的几个脚本放在一起后来源,我最终得到了如下结果:
# INIT Script
######################################
# Written by Juan Carlos Gonzalez.
# Modified by Joseph Kodjo-Kuma
# Name of app ( codingpains, chopent we, etc )
APP=gs-spring
# Name of the user to run as
USER=javaappuser
# Location of application's details
JAR_FULL_PATH=/home/vagrant/gs-spring-boot-0.1.0.jar
JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-amd64"
PID_PATH_NAME=/tmp/gs-spring-app.pid
case "$1" in
start)
echo "Starting $APP !!!"
if [ ! -f $PID_PATH_NAME ]; then
/bin/su - $USER -c "export JAVA_HOME=$JAVA_HOME"
/bin/su - $USER -c "/usr/bin/nohup java -server -Xms64m -Xmx128m -XX:MaxPermSize=128m -XX:+CMSClassUnloadingEnabled -jar $JAR_FULL_PATH /tmp 2>> /dev/null >> /dev/null" &
echo $! > $PID_PATH_NAME
echo "$APP started !!!"
else
echo "$APP is already running !!!"
fi
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "Stopping $APP !!!"
rm $PID_PATH_NAME
kill $PID
echo "$APP stopped successfully !!!"
#rm $PID_PATH_NAME
else
echo "$APP is not running !!!"
fi
;;
restart)
$0 stop
sleep 5
$0 start
;;
*)
echo "Usage: /etc/init.d/$APP {start|restart|stop}"
exit 1
;;
esac
exit 0
现在上述脚本可以工作了,服务已启动,但是当我尝试使用javaappuser
ps 验证它是否正在运行应用程序时,我发现有多个进程正在运行,这有点令人困惑。
vagrant@vagrant:~$ ps aux | grep java
root 2350 0.0 0.7 52544 3620 pts/0 S 09:19 0:00 /bin/su - javaappuser -c /usr/bin/nohup java -server -Xms64m -Xmx128m -XX:MaxPermSize=128m -XX:+CMSClassUnloadingEnabled -jar /home/vagrant/gs-spring-boot-0.1.0.jar /tmp 2>> /dev/null >> /dev/null
javaapp+ 2351 0.0 0.6 23344 3192 ? Ss 09:19 0:00 -su -c /usr/bin/nohup java -server -Xms64m -Xmx128m -XX:MaxPermSize=128m -XX:+CMSClassUnloadingEnabled -jar /home/vagrant/gs-spring-boot-0.1.0.jar /tmp 2>> /dev/null >> /dev/null
javaapp+ 2352 94.3 25.8 2051872 129308 ? Sl 09:19 0:02 java -server -Xms64m -Xmx128m -XX:MaxPermSize=128m -XX:+CMSClassUnloadingEnabled -jar /home/vagrant/gs-spring-boot-0.1.0.jar /tmp
vagrant 2365 0.0 0.4 16572 2320 pts/0 S+ 09:19 0:00 grep --color=auto java
现在我不确定它是否应该这样做,或者我肯定做错了什么
请帮助并提前致谢