Postgres systemd单元文件如何确定要运行的Postgres版本?

时间:2017-09-04 05:37:33

标签: postgresql systemd

这主要是出于好奇的问题。我正在浏览Postgres systemd单元文件,以了解systemd可以做些什么。 Postgres有两个systemd单元文件。用于代替系统目标的一个:

# systemd service for managing all PostgreSQL clusters on the system. This
# service is actually a systemd target, but we are using a service since
# targets cannot be reloaded.

[Unit]
Description=PostgreSQL RDBMS

[Service]
Type=oneshot
ExecStart=/bin/true
ExecReload=/bin/true
RemainAfterExit=on

[Install]
WantedBy=multi-user.target

第二个单元文件是Postgres版本参数化的模板。它指定它将使用Before=postgresql.service

在其他单元文件之前运行
# systemd service template for PostgreSQL clusters. The actual instances will
# be called "postgresql@version-cluster", e.g. "postgresql@9.3-main". The
# variable %i expands to "version-cluster", %I expands to "version/cluster".
# (%I breaks for cluster names containing dashes.)

[Unit]
Description=PostgreSQL Cluster %i
ConditionPathExists=/etc/postgresql/%I/postgresql.conf
PartOf=postgresql.service
ReloadPropagatedFrom=postgresql.service
Before=postgresql.service

[Service]
Type=forking
# @: use "postgresql@%i" as process name
ExecStart=@/usr/bin/pg_ctlcluster postgresql@%i --skip-systemctl-redirect %i start
ExecStop=/usr/bin/pg_ctlcluster --skip-systemctl-redirect -m fast %i stop
ExecReload=/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i reload
PIDFile=/var/run/postgresql/%i.pid
SyslogIdentifier=postgresql@%i
# prevent OOM killer from choosing the postmaster (individual backends will
# reset the score to 0)
OOMScoreAdjust=-900
# restarting automatically will prevent "pg_ctlcluster ... stop" from working,
# so we disable it here. Also, the postmaster will restart by itself on most
# problems anyway, so it is questionable if one wants to enable external
# automatic restarts.
#Restart=on-failure
# (This should make pg_ctlcluster stop work, but doesn't:)
#RestartPreventExitStatus=SIGINT SIGTERM

[Install]
WantedBy=multi-user.target

我无法弄清楚当我运行systemctl start postgresql时systemd如何确定要运行的Postgres版本。当我查看postgresql.service的依赖关系时,我看到有一个特定的模板实例,9.5-main,这是一个依赖项:

> systemctl list-dependencies postgresql

postgresql.service
● ├─postgresql@9.5-main.service
● ├─postgresql@9.5-main.service
● ├─system.slice
● └─sysinit.target
●   ├─apparmor.service
●   ├─brltty.service
●   ├─console-setup.service
●   ├─dev-hugepages.mount
●   ├─dev-mqueue.mount
●   ├─friendly-recovery.service
●   ├─keyboard-setup.service
●   ├─kmod-static-nodes.service
●   ├─lvm2-lvmetad.socket
●   ├─lvm2-lvmpolld.socket
●   ├─lvm2-monitor.service
●   ├─plymouth-read-write.service
●   ├─plymouth-start.service
●   ├─proc-sys-fs-binfmt_misc.automount
●   ├─resolvconf.service
●   ├─setvtrgb.service
●   ├─sys-fs-fuse-connections.mount
●   ├─sys-kernel-config.mount

我找不到任何指定应该使用Postgres 9.5的文件。没有任何其他Postgres单元文件,其他任何文件都没有提到Postgres。这是在Ubuntu 16.04上使用systemd 229和Postgres 9.5(通过sudo apt-get install postgresql安装)。

1 个答案:

答案 0 :(得分:2)

来自:https://www.freedesktop.org/software/systemd/man/systemd.unit.html

"%i"    Instance name   For instantiated units: this is the string between the "@" character and the suffix of the unit name.

文件名是postgres@9.3-main.service,%i = 9.3-main

更新:

实例文件通常作为模板文件的符号链接创建,链接名称包含实例标识符

file /run/systemd/generator/postgresql.service.wants/postgresql@9.4-main.service
/run/systemd/generator/postgresql.service.wants/postgresql@9.4-main.service: symbolic link to /lib/systemd/system/postgresql@.service