Skip to content

Custom systemd services

Fire script after docker.service is up

Systemd
[Unit]
Description=scriptName or description
After=docker.service
BindsTo=docker.service
ReloadPropagatedFrom=docker.service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/someSript.sh
ExecReload=/usr/local/bin/someSript.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Java app example

Systemd
[Unit]
Description=Email service
Requires=network.target remote-fs.target
After=network.target remote-fs.target

[Service]
Type=simple

PIDFile=/path_to_PID/****.pid
User=ubuntu
Group=ubuntu
WorkingDirectory=/opt/apps/
ExecStart=/usr/bin/java -jar /opt/apps/email-1.0.0.jar

StandardOutput=append:/var/log/app.log
StandardError=append:/var/log/app-error.log

Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target

commands

  • Enable systemd service - it will create soft link
Bash
sudo systemctl enable custom.service
  • If you changed systemd config you need to reload it.
Bash
sudo systemctl daemon-reload
Bash
sudo systemctl start custom.service
Bash
sudo systemctl stop custom.service
Bash
sudo systemctl restart custom.service
Bash
sudo systemctl status custom.service