Skip to content

Services in Systemd

Services in Systemd

How to enable and disable services in  Systemd init

List of all services:

systemctl
To list all loaded services on your system (whether active; running, exited or failed, use the list-units subcommand and --type switch with a value of service.
systemctl list-units --type=service
OR
systemctl --type=service

To start a service in systemd run the command as shown:

systemctl start service-name

For example, to start apache web service, run

systemctl start apache2

To verify that the service is running, run

systemctl status apache2

Output

● apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Thu 2018-03-15 17:09:05 UTC; 35s ago
     Docs: man:systemd-sysv-generator(8)
   CGroup: /system.slice/apache2.service
           ├─2499 /usr/sbin/apache2 -k start
           ├─2502 /usr/sbin/apache2 -k start
           └─2503 /usr/sbin/apache2 -k start

Mar 15 17:09:04 ip-172-31-41-251 systemd[1]: Starting LSB: Apache2 web server...
Mar 15 17:09:04 ip-172-31-41-251 apache2[2475]:  * Starting Apache httpd web ser
Mar 15 17:09:05 ip-172-31-41-251 apache2[2475]:  *
Mar 15 17:09:05 ip-172-31-41-251 systemd[1]: Started LSB: Apache2 web server.

To stop the service running service

systemctl stop apache2

To confirm that the service is not running, run

systemctl status apache2

Output

● apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: inactive (dead) since Thu 2018-03-15 17:19:47 UTC; 12s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 2822 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS
  Process: 2687 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCE

Mar 15 17:10:11 ip-172-31-41-251 systemd[1]: Starting LSB: Apache2 web server...
Mar 15 17:10:11 ip-172-31-41-251 apache2[2687]:  * Starting Apache httpd web ser
Mar 15 17:10:12 ip-172-31-41-251 apache2[2687]:  *
Mar 15 17:10:12 ip-172-31-41-251 systemd[1]: Started LSB: Apache2 web server.
Mar 15 17:19:46 ip-172-31-41-251 systemd[1]: Stopping LSB: Apache2 web server...
Mar 15 17:19:46 ip-172-31-41-251 apache2[2822]:  * Stopping Apache httpd web ser
Mar 15 17:19:47 ip-172-31-41-251 apache2[2822]:  *
Mar 15 17:19:47 ip-172-31-41-251 systemd[1]: Stopped LSB: Apache2 web server.

To enable apache2 service on boot up run

systemctl enable apache2

To disable apache2 service on boot up run

systemctl disable apache2

To restart the service

systemctl restart apache2

To check whether the service is currently configured to start on the next boot up

systemctl is-enabled apache2

Output

Executing /lib/systemd/systemd-sysv-install is-enabled apache2
enabled

To check whether the service is active

systemctl is-active apache2

Output

active

How to remove Systemd services completely

What if you installed a package, and later on decide that you don't need it anymore. How do you go about removing it completely? Follow the commands below.

First, stop the service

systemctl stop service-name

Then disable the service

systemctl disable service-name

Removing the service in systemd

rm /etc/systemd/system/service-name
rm /etc/systemd/system/service-name/[related symlinks]

Reload systemd

systemctl daemon-reload

Finally run,

systemctl reset-failed

Source Source