-1

我想systemdpgagnent.

init.d在此页面http://technobytz.com/automatic-sql-database-backup-postgres.html上只找到了脚本,但我不知道如何start-stop-daemon在 systemd 中执行。

我已经写了那个单元:

[Unit]
Description=pgagent
After=network.target postgresql.service

[Service]
ExecStart=start-stop-daemon -b --start --quiet --exec pgagent --name pgagent --startas pgagent -- hostaddr=localhost port=5432 dbname=postgres user=postgres
ExecStop=start-stop-daemon --stop --quiet -n pgagent 


[Install]
WantedBy=multi-user.target

但我收到如下错误:

[/etc/systemd/system/pgagent.service:14] Executable path is not absolute, ignoring: start-stop-daemon --stop --quiet -n pgagent

那个单位有什么问题?

4

2 回答 2

0

如果您使用yumor安装了 pgagent apt-get,它应该已经为您创建了 systemd 文件。例如,在 RHEL 7(本质上是 CentOS 7)上,您可以安装 PostgreSQL 12,然后安装 pgagent

sudo yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install postgresql12
sudo yum install postgresql12-server
sudo yum install pgagent_12.x86_64

这会将 PostgreSQL/var/lib/pgsql/12和 pgagent_12 安装到/usr/bin/pgagent_12

此外,它还会在以下位置创建一个 systemd 文件/usr/lib/systemd/system/pgagent_12.service

查看服务的状态systemctl status pgagent_12

将其配置为自动启动,然后启动它,使用:

sudo systemctl enable pgagent_12
sudo systemctl start pgagent_12

很可能身份验证会失败,因为默认的 .service 文件有

ExecStart=/usr/bin/pgagent_12 -s ${LOGFILE} hostaddr=${DBHOST} dbname=${DBNAME} user=${DBUSER} port=${DBPORT}

确认sudo tail /var/log/pgagent_12.log将显示

Sat Oct 12 19:35:47 2019 WARNING: Couldn't create the primary connection [Attempt #1]
Sat Oct 12 19:35:52 2019 WARNING: Couldn't create the primary connection [Attempt #2]
Sat Oct 12 19:35:57 2019 WARNING: Couldn't create the primary connection [Attempt #3]
Sat Oct 12 19:36:02 2019 WARNING: Couldn't create the primary connection [Attempt #4]

为了解决问题,我们需要创建一个在服务启动时可以访问的 .pgpass 文件。一、停止服务

sudo systemctl stop pgagent_12

检查服务文件并less /usr/lib/systemd/system/pgagent_12.service显示它有

User=pgagent
Group=pgagent

此外,/etc/pgagent/pgagent_12.conf

DBNAME=postgres
DBUSER=postgres
DBHOST=127.0.0.1
DBPORT=5432
LOGFILE=/var/log/pgagent_12.log

检查/etc/passwd文件以查找 pgagent 用户及其主目录:grep "pgagent" /etc/passwd

pgagent:x:980:977:pgAgent Job Schedule:/home/pgagent:/bin/false

因此,我们需要创建一个 .pgpass 文件/home/pgagent/.pgpass来定义 postgres 用户的密码

sudo su -
mkdir /home/pgagent
chown pgagent:pgagent /home/pgagent
chmod 0700 /home/pgagent
echo "127.0.0.1:5432:postgres:postgres:PasswordGoesHere" > /home/pgagent/.pgpass
chown pgagent:pgagent /home/pgagent/.pgpass
chmod 0600 /home/pgagent/.pgpass

目录和文件权限很重要。如果您遇到问题,您可以通过编辑服务文件/usr/lib/systemd/system/pgagent_12.service来启用调试日志记录,以通过更新ExecStart命令来 启用调试日志记录-l 2

ExecStart=/usr/bin/pgagent_12 -l 2-s ${LOGFILE} hostaddr=${DBHOST} dbname=${DBNAME} user=${DBUSER} port=${DBPORT}

更改 .service 文件后,必须重新加载内容sudo systemctl daemon-reload(如果您忘记了这个要求,systemd 会通知您)。

继续启动/停止服务并检查/var/log/pgagent_12.log最终,它将正常启动并sudo systemctl status pgagent_12显示

● pgagent_12.service - PgAgent for PostgreSQL 12
   Loaded: loaded (/usr/lib/systemd/system/pgagent_12.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-10-12 20:18:18 PDT; 13s ago
  Process: 6159 ExecStart=/usr/bin/pgagent_12 -s ${LOGFILE} hostaddr=${DBHOST} dbname=${DBNAME} user=${DBUSER} port=${DBPORT} (code=exited, status=0/SUCCESS)
 Main PID: 6160 (pgagent_12)
    Tasks: 1
   Memory: 1.1M
   CGroup: /system.slice/pgagent_12.service
           └─6160 /usr/bin/pgagent_12 -s /var/log/pgagent_12.log hostaddr=127.0.0.1 dbname=postgres user=postgres port=5432

Oct 12 20:18:18 prismweb3 systemd[1]: Starting PgAgent for PostgreSQL 12...
Oct 12 20:18:18 prismweb3 systemd[1]: Started PgAgent for PostgreSQL 12.
于 2019-10-13T03:26:00.727 回答
0

systemd 期望 ExecStart 和 ExecStop 命令包含可执行文件的完整路径。

systemd 管理下的服务不需要 start-stop-daemon。你会想让它执行底层的 pgagent 命令。

https://unix.stackexchange.com/questions/220362/systemd-postgresql-start-script为例

于 2018-09-04T22:59:40.963 回答