How To Use Systemctl to Manage Systemd Services and Units
Hey there! In this blog post, I'm going to be diving into how we can use systemctl to manage and view services that are running on a linux based system.
Essentially, systemctl is a management tool that interacts with systemd - and init system. An init system's job is to start and manage the components that need to be started after the Linux kernel is booted. These are mostly userspace components.The init system is also used to manage services and daemons for the server at any point while the system is running.
For service management tasks, the target unit will be service units, which have unit files with a suffix of .service. However, for most service management commands, you can actually leave off the .service suffix, as systemd is smart enough to know that you probably want to operate on a service when using service management commands.
Starting and Stopping of services
To start a systemd service, executing instructions in the service’s unit file, use the start command. If you are running as a non-root user, you will have to use sudo since this will affect the state of the operating system:
$ sudo systemctl start application.service
OR
$ sudo systemctl start application
Restarting and Reloading of a service
The restart and reload command command can be used to restart or reload the services. If you are unsure whether the service has the functionality to reload its configuration, you can issue the reload-or-restart command. This will reload the configuration in-place if available. Otherwise, it will restart the service so the new configuration is picked up:
$ sudo systemctl restart application.service
$ sudo systemctl reload application.service
$ sudo systemctl reload-or-restart application.service
Starting a service at boot time
The above commands are useful for starting or stopping services during the current session. To tell systemd to start services automatically at boot, you must enable them.
If you want to stop services from running at boot time, use the disable command.
$ sudo systemctl enable application.service
$ sudo systemctl disable application.service
This will create a symbolic link from the system’s copy of the service file (usually in /lib/systemd/system or /etc/systemd/system) into the location on disk where systemd looks for autostart files (usually /etc/systemd/system/some_target.target.wants.
Checking the status of a service
To check the status of a service on your system, you can use thestatuscommand:
$ systemctl status application.service
This will provide you with the service state, the cgroup hierarchy, and the first few log lines.
Masking and unmasking a service
Masking renders service as completely unstartable, automatically or manually, by linking it to /dev/null. For example:
$ sudo systemctl mask nginx.service
This will prevent the Nginx service from being started, automatically or manually, for as long as it is masked. If you attempt to start the service, you will see a message like this:
$ sudo systemctl start nginx.service
Failed to start nginx.service: Unit nginx.service is masked.
To unmask a unit, making it available for use again, use the unmask command:
$ sudo systemctl unmask nginx.service
This will return the unit to its previous state, allowing it to be started or enabled.
Source: https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units
I hope that this has been helpful to you! ☺️