Start Java jar by service (linux) with parameters

2019-07-28 14:58发布

I want to start a jar file (Spring Boot app), with additional parameters.

It start normally when I start by shell command:

java -jar spring.boot.jar --parameter01="My Parameter Value 01" --parameter02="My Parameter Value 02"

Now, I want to pass all parameters into a bash shell (to start it as a service within /etc/init.d)

java -jar spring.boot.jar $1

($1 is equals value above >> --parameter01="My Parameter Value 01" --parameter02="My Parameter Value 02")

Then Spring app doesn't recognize the value "My Parameter Value 01", but only "My". What should I do ? I tried to escape the space character and quote character, but nothing work.

标签: java bash shell
2条回答
Lonely孤独者°
2楼-- · 2019-07-28 15:37

I think you could try if your system supports systemd:

Create a service file with a name you consider is appropriate for you in the /etc/systemd/system/ directory:

sudo vi /etc/systemd/system/spring-boot.service

[Unit]
Description=Spring Boot Service
StartLimitIntervalSec=0
After=syslog.target

[Service]
Type=simple
Restart=always
RestartSec=4
User=root
ExecStart=/bin/bash -c "/usr/bin/java -jar spring.boot.jar --parameter01='My Parameter Value 01' --parameter02='My Parameter Value 02'"

[Install]
WantedBy=multi-user.target

Next you have to reload systemd:

sudo systemctl daemon-reload

Next you can start it:

sudo systemctl start spring-boot

Get status:

systemctl status spring-boot

Stop it:

sudo systemctl stop spring-boot

Or add it to autostart with system booting:

sudo systemctl enable spring-boot

To troubleshot you could run the next in parallel terminal window to see the realtime changes:

sudo tail -f /var/log/syslog
查看更多
不美不萌又怎样
3楼-- · 2019-07-28 15:38

A simple script to call your java -jar spring.boot.jar with a single parameter, let's call it /etc/init.d/run-spring-boot, be sure to chmod +x /etc/init.d/run-spring-boot before executing it:

#!/bin/bash
echo java -jar spring.boot.jar $1
java -jar spring.boot.jar $1

Invoke the script with a single input parameter of the entire string quoted and with escaped inner quotation marks:

/etc/init.d/run-spring-boot "--parameter01=\"My Parameter Value 01\" --parameter02=\"My Parameter Value 02\""
查看更多
登录 后发表回答