Systemd is an init system and system manager that has widely become the new standard for Linux distributions.
Prerequisites
For this tutorial you need:
- An active Fuga Cloud account
- Any Linux distro.
Service Management
The fundamental purpose of an init system is to initialise the components that must be started after the Linux kernel is booted. The init system is also used to manage services and daemons for the server at any point, while the system is running. With that in mind, lets start with some basic service management operations.
Start and Stop Service
If you want to start a service, you use the start command. You need to use sudo because it affects the state of your system:
$ sudo systemctl start apache2
As you can see, you have started the Apache service. If you wish to stop the apache services, you can replace the start command into stop, as seen in the following example:
$ sudo systemctl stop apache2
Restart and Reload Service
To restart the service, you use the following command:
$ sudo systemctl restart apache2
If you wish to reload the service, you use the reload command:
$ sudo systemctl reload apache2
If you are not sure the service has to be reloaded or restarted, you can use both statements:
$ sudo systemctl reload-or-restart apache2
This will reload the configuration, if available. If not, the service will restart so new configurations will be added to apache.
Enable and Disable Service
You have just learned how to start and stop any service. To start a service at boot, you need to enable it.
$ sudo systemctl enable apache2
To disable the service from starting at boot, you use the following command:
$ sudo systemctl disable apache2
When enabling or disabling a service, it does not start at the current session. You must use the start or stop statement, as you have used earlier.
Check the status of your service
To check the status of the service you are working with, use the status command:
$ sudo systemctl status apache2
You will receive the following output:
apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2022-03-24 15:19:10 UTC; 25min ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 23884 (apache2)
Tasks: 55 (limit: 8908)
Memory: 5.6M
CGroup: /system.slice/apache2.service
├─23884 /usr/sbin/apache2 -k start
├─23974 /usr/sbin/apache2 -k start
└─23975 /usr/sbin/apache2 -k start
Mar 24 15:19:10 mysql systemd[1]: Starting The Apache HTTP Server...
This will give you an overview of the current status of the service you are running. As well as problems you are facing with which might require to take action.
System Status overview
You have learned how to manage a single service. If you want to get an overview of all the active services in your system, you can use the list-units command.
$ sudo systemctl list-units
You receive the following output:
UNIT LOAD ACTIVE SUB DESCRIPTION
accounts-daemon.service loaded active running Accounts Service
apache2.service loaded active running The Apache HTTP Server
apparmor.service loaded active exited Load AppArmor profiles
apport.service loaded active exited LSB: automatic crash report generation
atd.service loaded active running Deferred execution scheduler
blk-availability.service loaded active exited Availability of block devices
...
...
...
If you wish the view all services, you can add the --all flag:
$ sudo systemctl list-units --all
You can use other flags to filter the output. Use the --state= flag to identify LOAD, ACTIVE, or SUB states as seen in the column above. An example would be:
$ systemctl list-units --state=active
$ systemctl list-units --state=loaded
List all unit files
To see every available unit file within the systemd paths, including those that systemd has not attempted to load, you can use the list-unit-files command:
$ sudo systemctl list-unit-files
Output example:
UNIT FILE STATE VENDOR PRESET
proc-sys-fs-binfmt_misc.automount static enabled
-.mount generated enabled
boot-efi.mount generated enabled
dev-hugepages.mount static enabled
dev-mqueue.mount static enabled
proc-sys-fs-binfmt_misc.mount disabled enabled
snap-core20-1169.mount enabled enabled
snap-core20-1376.mount enabled enabled
...
...
...
Conclusion
In this tutorial you have learned how to start and stop any service in your system, by using Systemclt. You also learned how to enable and disable these services, and view the status of each service in your system.