Skip to content

Instantly share code, notes, and snippets.

@gelin
Created February 20, 2020 15:40
Show Gist options
  • Save gelin/50a3849419957c186c3753535dab941c to your computer and use it in GitHub Desktop.
Save gelin/50a3849419957c186c3753535dab941c to your computer and use it in GitHub Desktop.
Java Systemd Service on Debian Jessie

Java Systemd Service on Debian Jessie

Systemd

Make sure systemd is installed.

systemctl

This command should list all installed services.

Install Systemd

If systemd is not active in your system, you should install it.

apt update
apt install systemd systemd-sysv
reboot

Note this changes the default init system on your server. Do it only if this is a new fresh server under your full control.

Systemd unit

Assuming you have a Java service to be started automatically by systemd.

The service name is java_service.

It's located in /opt/java_service folder and can be started just with java -jar server.jar.

It prints logs to stdout.

It wants to be run as java_user user.

Preparations

Create the system user for your service.

adduser --system java_user

Copy files of your service to /opt/java_service.

cd /opt
mkdir java_service
# cp ...

Change permissions.

chown -R java_user /opt/java_service

Unit file

Create unit file named /etc/systemd/system/java_service.service with the following content.

[Unit]
Description=Java service        
# put more actual description here

[Service]
Type=simple
WorkingDirectory=/opt/java_service
ExecStart=/usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar server.jar    
# path to java depends on your JDK requirements, in many cases it's enough to use just 'java'
User=java_user

[Install]
WantedBy=multi-user.target

Enable the unit to be autostarted after reboot.

systemctl enable java_service

If you modify the unit file later you have to reload it to systemd.

systemctl daemon-reload

Service control

Start service.

systemctl start java_service

Check service status (also shows last stdout entries).

systemctl status java_service

Stop service.

systemctl stop java_service

See more complete log (stdout) of the service.

journalctl -u java_service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment