Linux pgagent系统单元

Linux pgagent系统单元,linux,centos,systemd,pgagent,Linux,Centos,Systemd,Pgagent,我想为pgagnent制作一个systemd装置 我在这个页面上只找到了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 --sta

我想为
pgagnent
制作一个
systemd
装置

我在这个页面上只找到了
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

该单元有什么问题?

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

systemd管理下的服务不需要启动-停止守护程序。您需要让它执行底层pgagent命令


查看示例

如果您使用
yum
apt-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

使用
systemctl status pgagent\u 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]
● 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.
为了解决问题,我们需要创建一个.pgpass文件,该文件在服务启动时可以访问。首先,停止服务

sudo systemctl stop pgagent_12
使用
less/usr/lib/systemd/system/pgagent_12检查服务文件。服务
显示它已

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

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]
● 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.

Stack Overflow是一个关于编程和开发问题的网站。这个问题似乎离题了,因为它与编程或开发无关。请参见帮助中心中的。也许或者是一个更好的提问的地方。可能是因为它没有以postgres用户的身份运行吗?另外,如果要使用unix套接字和postgres超级用户,我认为不需要传递hostaddr=localhost。我对您的体验非常感兴趣,因为我也想让pgagent以服务的形式运行:)请避免回答问题。相反,关闭它,继续前进。有时,建议一个问题可能与主题相关的站点是有帮助的。