Skip to content

Running as systemd service

Ahmet Türkmen edited this page Apr 16, 2020 · 3 revisions

Create a user named hknd and and add the following system service configuration to /lib/systemd/system/hknd.service:

[Unit]
Description=Haaukins (Daemon)
ConditionPathExists=/home/hknd/daemon
After=network.target

[Service]
Type=simple
User=hknd
Group=hknd
LimitNOFILE=1024

Restart=on-failure
RestartSec=10
StartLimitIntervalSec=60
TimeOutStopSec=300

WorkingDirectory=/home/hknd/daemon
ExecStart=/home/hknd/daemon/hknd

# removing leftover docker-containers and virtualmachines
ExecStopPost=/bin/bash /home/hknd/daemon/clean-up.sh

# make sure log directory exists and owned by syslog
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p /var/log/hknd
ExecStartPre=/bin/chown root:adm /var/log/hknd
ExecStartPre=/bin/chmod 755 /var/log/hknd
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=hknd

[Install]
WantedBy=multi-user.target

In some cases, some vms and docker containers might leftover, which means that even though Haaukins service has been stopped by running following command:

`systemctl stop hknd` 

there could be some number of vms and docker containers which are running (although they should not). For such a situation, ExecStopPost script should be included into service file as well. The script for cleaning up leftovers from Haaukins (- after Haaukins service is stopped-) is given below;

  • clean-up.sh
#!
# Killing containers which has label of "hkn"
docker kill $(docker ps -q --filter "label=hkn")
# Removing killed containers which have label of "hkn"
docker rm $(docker ps -q -a --filter "label=hkn" --filter status=exited)
# Prune Network bridges which are not used by any container
docker system prune -f  
docker network prune -f

docker volume prune -f
# Close running vms all
VBoxManage list runningvms | awk '{print $2;}' | xargs -I vmid VBoxManage controlvm vmid poweroff
## Unregister VMs ID
VBoxManage list vms | awk '{print $2;}' | xargs -I vmid VBoxManage unregistervm --delete vmid

rm -rf /home/hknd/VirtualBox\ VMs/*
# Cleanup previously imported VMDKs path

Note that some paths could differ according to your system.

Clone this wiki locally