Skip to content

Instantly share code, notes, and snippets.

@zsiegel
Last active May 9, 2020 01:20
Show Gist options
  • Save zsiegel/fb987c258d01b753b649ab6156dedbf1 to your computer and use it in GitHub Desktop.
Save zsiegel/fb987c258d01b753b649ab6156dedbf1 to your computer and use it in GitHub Desktop.
systemd example - works on Ubuntu 16.04

Ubuntu 16.04 systemd service setup

Enable persistent user systemd services

loginctl enable-linger $user

Create a user systemd directory

mkdir -p ~/.config/systemd/user

Create a systemd configuration

touch my-service.service

Example service

[Unit]
Description=myservice
After=network.target

[Service]
EnvironmentFile=/home/user/.myservice_env
Restart=always
RestartSec=5
ExecStart=/usr/bin/java -jar /home/user/my-service.jar

[Install]
WantedBy=multi-user.target
# WantedBy=default.target *use this if you want to start on boot*

For more info and details of options checkout this

Reload the user daemon (this must be done on every change to your service file)

systemctl --user daemon-reload

Starting and or stop your service as needed

systemctl --user [start|stop|restart] my-service
@zsiegel
Copy link
Author

zsiegel commented Mar 21, 2017

If you are having trouble getting things to work on boot checkout this

$ systemctl --user disable my-service
$ systemctl --user daemon-reload
$ systemctl --user enable my-service

@zsiegel
Copy link
Author

zsiegel commented Mar 21, 2017

To check if your user service is setup as a target of boot use

systemctl --user list-dependencies default.target

@zsiegel
Copy link
Author

zsiegel commented Mar 21, 2017

Also see this systemd github issue for debugging help

@hetzbh
Copy link

hetzbh commented May 7, 2020

Small correction: you should use -p in mkdir. Example: mkdir -p ~/.config/systemd/user

@zsiegel
Copy link
Author

zsiegel commented May 9, 2020

Small correction: you should use -p in mkdir. Example: mkdir -p ~/.config/systemd/user

Good call. Updated.

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