在Linux上讲Java命令行的作为服务运行
Posted 邓志国
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Linux上讲Java命令行的作为服务运行相关的知识,希望对你有一定的参考价值。
现在Spring等框架可以支持别用War发布而是用Jar发布,这样服务器就不用安装容器了,方便不少。但不安装容器就需要自己来写脚本完成服务的启动、停止、重启。
脚本如下:
#!/bin/bash # # chkconfig: 345 70 30 # description: ${description} # processname: ${processName} # Source function library. #!/bin/sh SERVICE_NAME=${serviceName} PATH_TO_JAR=${jarFile} PID_PATH_NAME=/tmp/${serviceName}-pid case $1 in start) echo "Starting $SERVICE_NAME ..." if [ ! -f $PID_PATH_NAME ]; then nohup java -jar ${javaArgs} $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null & echo $! > $PID_PATH_NAME echo "$SERVICE_NAME started ..." else echo "$SERVICE_NAME is already running ..." fi ;; stop) if [ -f $PID_PATH_NAME ]; then PID=$(cat $PID_PATH_NAME); echo "$SERVICE_NAME stoping ..." kill $PID; echo "$SERVICE_NAME stopped ..." rm $PID_PATH_NAME else echo "$SERVICE_NAME is not running ..." fi ;; restart) if [ -f $PID_PATH_NAME ]; then PID=$(cat $PID_PATH_NAME); echo "$SERVICE_NAME stopping ..."; kill $PID; echo "$SERVICE_NAME stopped ..."; rm $PID_PATH_NAME echo "$SERVICE_NAME starting ..." nohup java -jar ${javaArgs} $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null & echo $! > $PID_PATH_NAME echo "$SERVICE_NAME started ..." else echo "$SERVICE_NAME is not running ..." fi ;; esac
以上是关于在Linux上讲Java命令行的作为服务运行的主要内容,如果未能解决你的问题,请参考以下文章