Recently I needed to create custom server service on Debian Jessie (wich is version 8) and there is systemd used as a service system. Systemd is very complex but if you are not an expert, this brief article will show you simple way for creating general server service.
Topic: Create own systemd service
OS: Debian 8 (Jessie)
Tool: systemctl, service, vim
List services
“systemctl” is basic command to work with systemd service. When executes, it lists all services. Just type:
systemctl
Create service
Service definition files (on Debian 8) are located under /lib/systemd/system/. We just need to create a new one. Our newly created file will be /lib/systemd/system/myservice.service
[Unit]
Description=My custom service
After=network.target[Service]
Type=forking
PIDFile=/run/myservice.pid
ExecStart=/root/bin/myServiceStart.sh
ExecStop=/root/bin/myServiceStop.sh
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=myservice[Install]
WantedBy=multi-user.target
Referenced sh scripts contain server execution instructions, “Type forking” provides daemon like process execution usually used with optional PIDFile. Here we need network service as a prerequisite and we expect scripts output available in syslog. This definition should work well for some terminal server service executed from script files. If you have another type of service, you’ll have to modify it according to your specific needs.
Enable service
Here we are using “systemctl” but we can also use “service” command for hese obvious operations.
systemctl enable myservice
Disable service
systemctl disable myservice
Stop service
systemctl stop myservice
# or
service myservice stop
Start service
systemctl start myservice
# or
service myservice start
Service logging and status
To see log messages you can just read syslog content:
cat /var/log/syslog
We can also check service status like this:
systemctl status myservice
That’s all, everything should work now. For more about systemd, check man pages.
Slaves, obey your earthly masters with fear and trembling, with a sincere heart, as you would Christ, not by the way of eye-service, as people-pleasers, but as servants of Christ, doing the will of God from the heart, rendering service with a good will as to the Lord and not to man, knowing that whatever good anyone does, this he will receive back from the Lord, whether he is a slave or free. (Ephesians 6:5)