How to Create a new service with systemd

create a new service with systemd

This tutorial (create a new service with systemd) describes the steps to follow to turn a program into a systemd service that can be started automatically during system startup. For our example, we will create a service to start the daemon Deluge start ( deluged ), but it can be applied to any program.

Prerequisites

  • Have the administrative rights
  • Knowing how to use the terminal

How to create a new service with systemd

Creating a program encapsulating script

Deluged does not support stopping or stimulus parameters, we need to create a script that will do that. We can store the script in / usr / local / bin

sudo cat> /usr/local/bin/deluge.sh

Then paste the following code:

#! / bin / bash 
# /usr/local/bin/deluge.sh 
#
 
function d_start ( ) 
{ 
	echo  "Deluge: starting service" 
	deluged --pidfile = / tmp / deluge.pid
	 sleep  5 
	echo  "PID is $ (cat /tmp/deluge.pid) " 
}
 
function d_stop ( ) 
{ 
	echo  "Deluge: stopping Service (PID = $ (cat /tmp/deluge.pid) )" 
	kill $ ( cat  / tmp / deluge.pid ) 
	rm  / tmp / deluge.pid
 }
 
function d_status ( ) 
{ 
	ps  -ef  |  grep deluged |  grep  -v  grep 
	echo  "PID indicate indication file $ (cat /tmp/deluge.pid 2> / dev / null) " 
}
 
# Some Things That run always 
touch  / var / lock / deluge
 
# Management instructions of the service 
box  "$ 1"  in 
	start )
		d_start
		;; 
	Stop )
		d_stop
		;; 
	Reload )
		d_stop
		sleep  1
		d_start
		;; 
	Status )
		d_status
		;; 
	* ) 
	Echo  "Usage: $ 0 {start | stop | reload | status}" 
	exit  1 
	;; 
esac
 
exit  0

Create the service file

In Systemd, services are defined in files tickets · located in / etc / systemd / system / .

Create a service package for our program:

sudo cat> /etc/systemd/system/deluge.service

Then paste the following code:

[Unit]
Description = Deluge daemon
After network.target = auditd.service

[Service]
Type = forking
ExecStart = / usr / local / bin / start deluge.sh
ExecStop = / usr / local / bin / stop deluge.sh
ExecReload = / usr / local / bin / reload deluge.sh

[Install]
WantedBy = multi-user.target

Service Installation

We must now reload the list of services:

sudo systemctl daemon reload

Then activate the launch of the service at boot:

sudo systemctl enable deluge

Key services management commands

For reference, the following commands to manage the service:

In our example, <service> == deluge

systemctl sudo 
order Description
start Start Service
stop Stop Service
status Service Status
is-enabled Whether the service is activated at boot
enable Active start the service
disable Disables the startup service

Conclusion

Please note, services are launched by root , so if you have already configured the program with another user, it will:

  • either copy the configuration to root ,
  • or point the service to the proper configuration (for deluged there is an option -config )

So, it remains only to test that everything is working by running the service:

sudo systemctl start deluge

If everything can restart the machine to check that the service starts automatically:

sudo reboot

You May Also Like

About the Author: admin

Leave a Reply

Your email address will not be published.