Sub topic 7
Running the JAR as a systemd service
This chapter assumes you can build an executable JAR and run it with java -jar. It also assumes you have a Linux server with systemd and basic comfort with sudo, file permissions, and environment variables. Configuration and shutdown behavior come from earlier chapters.
Why not just run java -jar in a terminal
A JAR started from an SSH session dies when the session ends. It does not start after a reboot. It does not restart after a crash. systemd is the process manager on most Linux distributions. You describe the application in a unit file, and systemd starts it at boot, restarts it on failure, and collects its logs.
Prepare the server
Create an unprivileged account for the application. It should have no login shell, so a compromise of the app does not give an attacker a general-purpose user.
sudo useradd --system --no-create-home --shell /usr/sbin/nologin myapp
sudo mkdir -p /opt/myapp /etc/myapp
sudo cp target/myapp-1.0.0.jar /opt/myapp/app.jar
sudo chown myapp:myapp /opt/myapp/app.jarPut the environment variables in /etc/myapp/env, one NAME=value per line, with no export and no quotes unless the value needs them.
sudo tee /etc/myapp/env > /dev/null <<'EOF'
DB_URL=jdbc:postgresql://db.internal:5432/myapp
DB_PASSWORD=change-me
SPRING_PROFILES_ACTIVE=prod
EOF
sudo chown root:myapp /etc/myapp/env
sudo chmod 640 /etc/myapp/envThe file is readable by root and the myapp group only. This keeps the database password out of the JAR and out of the process list, which --spring.datasource.password=... on the command line would not.
The unit file
Save this as /etc/systemd/system/myapp.service.
[Unit]
Description=My Spring Boot app
After=network.target
[Service]
User=myapp
EnvironmentFile=/etc/myapp/env
ExecStart=/usr/bin/java -jar /opt/myapp/app.jar
SuccessExitStatus=143
Restart=on-failure
[Install]
WantedBy=multi-user.target| Setting | Purpose |
|---|---|
| `After=network.target` | Start after networking is up. |
| `User=myapp` | Run as the unprivileged account. |
| `EnvironmentFile` | Load configuration at start. Spring Boot reads `DB_URL` and the others from the environment. |
| `ExecStart` | The command to run. Use absolute paths. |
| `SuccessExitStatus=143` | Treat exit code 143 as success. |
| `Restart=on-failure` | Restart after a crash or non-zero exit. |
| `WantedBy=multi-user.target` | Start at normal boot when enabled. |
The exit code 143 is 128 plus 15, the signal number of SIGTERM. When you stop the service, systemd sends SIGTERM. The JVM shuts down and exits with 143. Without SuccessExitStatus=143, systemd records every normal stop as a failure.
Shutdown ties back to graceful shutdown
systemd waits for the process to exit after SIGTERM, then sends SIGKILL after TimeoutStopSec, which is 90 seconds by default. Your Spring setting spring.lifecycle.timeout-per-shutdown-phase=30s fits inside that window. If you raise the Spring timeout above the systemd one, the process will be killed mid-drain, so keep the systemd value larger.
Start and inspect it
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
systemctl status myapp
journalctl -u myapp -f
curl http://localhost:8080/actuator/healthdaemon-reload makes systemd re-read unit files. enable registers the service for boot, and --now also starts it immediately. journalctl -u myapp -f follows the application's console output, which systemd captures.
Common errors
- Service fails with status 203/EXEC. The path in
ExecStartis wrong, orjavais not at/usr/bin/java. Check withwhich java. - Permission denied reading the JAR or env file. The
myappuser cannot read them. Check ownership and mode. - Placeholders such as
${DB_URL}fail to resolve. The variable is missing from the env file, or the file has a typo. Edit the file, then runsudo systemctl restart myapp. A change to the env file takes effect only on restart. - Restart loop.
Restart=on-failurerestarts a bad configuration forever. Readjournalctlfor the cause. AddingRestartSec=5slows the loop.
Exercise
You stop the service with systemctl stop, and the status shows failed with exit code 143. Which unit setting is missing, and why does the code 143 appear?
Show answer
SuccessExitStatus=143 is missing. systemd stops the service by sending SIGTERM, which has signal number 15. The JVM exits with 128 + 15 = 143, and without that setting systemd treats any non-zero code as a failure.
Exercise
Why is the database password placed in /etc/myapp/env with mode 640 instead of passed as a command-line argument in ExecStart?
Show answer
Command-line arguments are visible to other users in the process list, and the unit file is usually world-readable. The env file is readable only by root and the myapp group, so the secret stays out of both.