我想将我的Java应用程序作为Linux服务启动。我创建了一个包装文件:
SERVICE_NAME=ConfigurationService PATH_TO_JAR=/opt/Applications/Configuration-Service/configuration-service.jar PID_PATH_NAME=/tmp/ConfigurationService-pid case $1 in start) echo "Starting $SERVICE_NAME ..." if [ ! -f $PID_PATH_NAME ]; then nohup java -jar $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 $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
但是,当我用命令启动它时:
service ConfigurationService start
应用程序启动但无权连接到DataBase。当我调用我的Web API时,我可以看到这样的错误:
出现意外错误(type = Internal Server Error,status = 500)。 无法获得JDBC连接;嵌套异常是java.sql.SQLException:用户'root'@'localhost'拒绝访问(使用密码:YES)
如果我使用命令启动我的应用程序,它可以正常工作:
java -jar ConfigurationService.jar &
我知道它与用户和权限相关,但我是Linux配置的新手。感谢任何帮助