In this article we are going to take a look at systemctl and how to work with services from terminal from Ubuntu. The following lines may be of help to users who have just arrived at Gnu / Linux in general and in this case, Ubuntu in particular.
On Gnu / Linux systems, including Ubuntu, the utility systemctl can be used to control and manage services systemd. Systemd is made up of a set of daemons, libraries and tools that allow the administration and configuration of the system and interact with the kernel of the Gnu / Linux system.
Systemctl syntax
The syntax is the rule and format of how the systemctl command can be used. These syntax options can be reordered, but a format must be followed.
The following line shows an example of basic syntax for using the systemctl command:
systemctl [OPCIONES] {COMANDO}
Options are flags that determine how commands are run or controlled or modify their behavior. The following is a list of some options that can be used with the systemctl command:
- - -state = STATE → With this option we will be able to enumerate units of a particular type of state of service: Active or Inactive.
- -a, - -all → We will use -ao - -all to show all properties / all drives currently in memory. To list all the units installed in the system, we will have to use the command 'list-unit-files' instead.
- -r, - -recursive → We will be able to use -ro - -recursive to display the list of host drives and local containers.
- -H - -host = [USER @] HOST → It will allow us operate on a remote host.
- is-system-running → We will verify if the system is fully working.
- hibernate → Hibernation of the System.
- - -help → It will show us the available options via the help message.
Systemctl examples
Next we will see some basic examples of how to execute and use systemctl on Ubuntu 18.04, which is the system I'm going to use for this example. We will simply have to run the systemctl command to make it work.
Start and stop services
For start services using the systemctl command, you just have to execute something like the following command:
sudo systemctl start application.service
We can also refer to the application name without the final .service. For stop service, the command to use will be something like:
sudo systemctl stop application.service
Restart and reload services
If you are looking for an restart service, you must write something like:
sudo systemctl restart application.service
For recharge the service, the command to use will be:
sudo systemctl reload application.service
Reloading a service only reloads configuration changes to a running service and will not completely restart the service. To completely restart a running service, the ideal is to use the option restart.
Enable and disable services
If we want to disable or enable a service, we will only have to use the following commands. Enabling a service will allow us to start automatically every time the server starts. To enable a service the command we should use should be something like:
sudo systemctl enable application.service
If we disable a service, the service will not run unless we re-enable it. To disable a service the command should be:
sudo systemctl disable application.service
Check the status of the service
To check the status of a service, you will have to use status option as follows:
sudo systemctl status application.service
List all services
For list all services that are running or are down, we can execute:
systemctl list-units --all --type=service --no-pager
The above command should list all the services and the output screen it will display will be similar to the previous screen shot. If we are interested see only all active services, we must use the following command:
systemctl list-units --all --state=active
For list all inactive services, the command to execute will be:
systemctl list-units --all --state=inactive
More information
For more information about using systemctl, we will only have to use help with the –help option or refer to the man pages:
man systemctl