Operations 4 min read

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.

360 Zhihui Cloud Developer
360 Zhihui Cloud Developer
360 Zhihui Cloud Developer
Run a Java JAR as a Systemd Service on Ubuntu with Auto‑Start

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.target

Step 2: Create a Bash script to launch the JAR

#!/bin/sh
sudo /usr/bin/java -jar my-webapp-1.0-SNAPSHOT.jar server config.yml

Make the script executable:

sudo chmod u+x my-webapp

Step 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-webapp

Step 4: View logs with journald

sudo journalctl --unit=my-webapp
sudo journalctl -f -n 1000 -u my-webapp
sudo systemctl stop my-webapp

Conclusion

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.

ServicebashUbuntusystemdjournald
360 Zhihui Cloud Developer
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.