Run a Java JAR as a Systemd Service on Ubuntu with Auto‑Start
This guide explains how to wrap a Java JAR into a systemd service on Ubuntu, create a startup script, enable automatic boot‑time launch, and monitor logs using journald, providing a reusable pattern for other languages and Linux distributions.
Introduction
When you need a Java JAR to run as a background service on Ubuntu and start automatically after a reboot, you can wrap it with a systemd service.
Step 1: Create the systemd service file
[Unit]
Description=My Webapp Java REST Service
[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/workspace
fileExecStart=/home/ubuntu/workspace/my-webapp
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.targetStep 2: Create a Bash script to launch the JAR
#!/bin/sh
sudo /usr/bin/java -jar my-webapp-1.0-SNAPSHOT.jar server config.ymlMake the script executable:
sudo chmod u+x my-webappStep 3: Enable and start the service
sudo systemctl daemon-reload
sudo systemctl enable my-webapp.service
sudo systemctl start my-webapp
sudo systemctl status my-webappStep 4: View logs with journald
sudo journalctl --unit=my-webapp
sudo journalctl -f -n 1000 -u my-webapp
sudo systemctl stop my-webappConclusion
This approach lets you run a Java program as a managed Ubuntu service, and the same pattern can be adapted for other languages or distributions.
360 Zhihui Cloud Developer
360 Zhihui Cloud is an enterprise open service platform that aims to "aggregate data value and empower an intelligent future," leveraging 360's extensive product and technology resources to deliver platform services to customers.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
