pgagent的系统单元

时间:2018-09-04 14:01:21

标签: linux centos systemd pgagent

我想为systemd制作一个pgagnent单位。

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

我写了那个单元:

[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

该单元怎么了?

2 个答案:

答案 0 :(得分:0)

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

start-stop-daemon对于systemd管理下的服务不是必需的。您将希望它执行基本的pgagent命令。

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

答案 1 :(得分:0)

如果用yumapt-get安装了pgagent,它应该已经为您创建了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

此外,它在/usr/lib/systemd/system/pgagent_12.service处创建一个systemd文件

使用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

因此,我们需要在/home/pgagent/.pgpass创建一个.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.