Make sure systemd is installed.
systemctl
This command should list all installed services.
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.
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.
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
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
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