To control the systemd system and service manager use the systemctl command. 1
Let's go ahead and see how to manage services using systemctl. 2
This is a very common sysadmin job. 3
First, using apt, I’ll install nginx, a well-known web server : I'll execute the many commands as root 4
so I'll become temporarily root in this terminal by running sudo su. 5
Execute each command as root by prefixing the command with sudo su or by becoming root as I did so apt 6
update && apt install nginx 7
If it's already installed, the command will either try to update it, if there is a new version, or does 8
nothing. 9
It's installing the Web server. 10
To find the nginx service status run: systemctl status nginx.service 11
This is displaying the service state and the first few log lines. Ngenix is active and running. By 12
the way, systemd is smart enough to know that you probably want to operate on a service and you can live 13
off that .service suffix. 14
So you can simply run systemctl status nginx without the service suffix. 15
It's the same! I'm connecting to the Web server, using the browser; it's running on localhost. 16
To stop the service run systemctl stop ngenix. 17
Now it's inactive or dead. Let's refresh the webpage. 18
It's not working anymore. To restart a running service 19
you can use the restart command. 20
So I'm startin ngenix systemctl start nginx 21
And now I'm restarting it systemctl restart nginx 22
This is very important since on Linux the configuration of a server is read 23
only when the server starts. 24
This means that if you change anything, you need to restart the server so that the new config comes 25
into effect. 26
If the application is able to reload its configuration files without restarting, you can issue the 27
reload command. 28
systemctl reload and the name of the service nginx; if you are unsure whether the service 29
has the functionality to reload its configuration without restarting, you can issue the reload or restart 30
command. 31
reload - or - restart 32
This will reload the configuration in place, if possible. Otherwise it will restart the service so that 33
the new configuration is read. 34
To configure a service, to start at boot time, use the enable command. 35
systemstl enable ngnix. 36
To check if a service will start automatically at boot time use the is enabled command. 37
OK, the command is is enabled, it ends in d and it's enabled. 38
To disable the service from starting automatically use the disable command. 39
systemsctl disable ngnix. 40
now it is disabled. 41
Keep in mind that enabling a service does not mean that it starts now, in the current session. 42
The start, stop and restart commands refer to the current session, what's happening to the service 43
now and the enable and disable commands refer to what will happen at boot time. For example 44
I can stop nginx now, but configure it to start automatically next time the system boots up 45
so systemctl stop nginx 46
systemctl enable nginx 47
Let's see if it's enabled. 48
It's enabled, so it will start automatically when the system boots up, but at the moment the service 49
is stopped. 50
Let's go ahead! To prevent the service from being started automatically or manually, use the mask command 51
systemctl mask nginx 52
Let's see its status. 53
It's masked; the command has marked the service unit as completely unstartable by linking it to slash 54
dev/null. 55
I cannot start it, systemsctl start nginx, and I can neither enable it. 56
Failed to enable unit, it's masked. 57
To unmask a unit, making it available again for use, simply utilize the unmask command: systemctl 58
unmask nginx and systemsctl start nginx. 59
it's running! 60
To see a list of all of the active units the systemd knows about, use the list-units 61
command 62
so systemctl list-units 63
These are all units; and to see all the unitss that systemd has loaded, regardless of whether they 64
are currently active or not, you can use the -all flag like this. 65
--all 66
OK, these are the most operations you'll perform on a service using systemctl! 67
Thank you!