Whenever I want to start up a CentOS server at work and put in a database or appliance and run it For example, if it was PostgreSQL
systemctl enable postgresql
systemctl start postgresql
And if it's a Docker daemon
systemctl enable docker
systemctl start docker
I do. I've used this "systemctl" command without thinking about it, I have an opportunity to investigate it properly, so I will summarize it here.
The processes that run on the OS can be broadly divided.
A process that is started by typing some command by yourself,
Processes that are up all the time the OS is up
There is. The former is, for example,
python hello.py
./start.sh
make install
Applies to. Since the user starts it, it is called user process </ b>.
You can check the running process by executing the ps
command.
The latter is a process that is launched by the system when the OS boots and runs in the background, which is called a daemon </ b>. Well-known ones are databases such as postgresql and mysql, reverse proxies such as httpd and nginx, and other appliances such as jenkins and github.
systemd
systemd is a process that starts these daemons and manages the process when the OS is started.
The command systemctl
accesses this systemd to start / stop and monitor daemons.
systemctl enable postgresql.service #Enable PostgreSQL server
systemctl restart docker #Restart the Docker daemon
systemctl stop nginx #Stop Nginx
systemctl status sshd #Check the status of the SSH daemon
So what does Systemd actually do when it's asked to start a daemon process?
It is described in a file called " unit file </ b>" under / etc / systemd / system /
.
For example, the contents of a PostgreSQL unit file are as follows.
Source: [https://www.postgresql.jp/document/11/html/server-start.html]
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
[Service]
Type=notify
User=postgres
ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0
(Omission)
The contents of the unit file are written like a Windows Config file (.ini).
The command that is actually executed is the ʻExecStart part of the
[Service] `section.
ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
In other words, Systemd is executing the command / usr / local / pgsql / bin / postgres -D / usr / local / pgsql / data
when the OS starts.
By the way, as it says ʻUser = postgres above it, this command is running as the
postgres` user.
Others include ʻAfter, which specifies the daemon that must be started before this unit, and ʻEnvironment
, which specifies an environment variable dedicated to the daemon.
The story around here is detailed below.
[https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-managing_services_with_systemd-unit_files:embed:cite]
You can also use systemctl list-unit-files
to display a list of unit files.
This time, I've summarized systemd, which actually played an important role in Linux. Nowadays, it is mainstream to use Docker or Kubernetes to scale and monitor by container or pod to publish the service, but when installing the purchased appliance etc., depending on the thing You may be forced to use such systemd. If something goes wrong there, it's a problem if you can't deal with it, so it's important to investigate it properly.
Recommended Posts