[英]systemd unit for pgagent
I want to make a systemd
unit for pgagnent
.我想为pgagnent
制作一个systemd
单元。
I found only init.d
script on this page http://technobytz.com/automatic-sql-database-backup-postgres.html , but I don't know how to exec start-stop-daemon
in systemd.我在这个页面http://technobytz.com/automatic-sql-database-backup-postgres.html上只找到了init.d
脚本,但我不知道如何在 systemd 中执行start-stop-daemon
。
I have written that unit:我已经写了那个单元:
[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
But I get errors like:但我收到如下错误:
[/etc/systemd/system/pgagent.service:14] Executable path is not absolute, ignoring: start-stop-daemon --stop --quiet -n pgagent
What is wrong with that unit?那个单位有什么问题?
systemd expects the ExecStart and ExecStop commands to include the full path to the executable. systemd 期望 ExecStart 和 ExecStop 命令包含可执行文件的完整路径。
start-stop-daemon is not necessary for services under systemd management.在 systemd 管理下的服务不需要 start-stop-daemon。 you will want to have it execute the underlying pgagent commands.您将希望让它执行底层的 pgagent 命令。
look at https://unix.stackexchange.com/questions/220362/systemd-postgresql-start-script for an example以https://unix.stackexchange.com/questions/220362/systemd-postgresql-start-script为例
If you installed pgagent with yum
or apt-get
, it should have created the systemd file for you.如果您使用yum
或apt-get
安装了 pgagent,它应该已经为您创建了 systemd 文件。 For example, on RHEL 7 (essentially CentOS 7), you can install PostgreSQL 12 followed by pgagent例如,在 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
This installs PostgreSQL to /var/lib/pgsql/12
and pgagent_12 to /usr/bin/pgagent_12
这会将 PostgreSQL 安装到/var/lib/pgsql/12
并将 pgagent_12 安装到/usr/bin/pgagent_12
In addition, it creates a systemd file at /usr/lib/systemd/system/pgagent_12.service
此外,它会在/usr/lib/systemd/system/pgagent_12.service
创建一个 systemd 文件
View the status of the service with systemctl status pgagent_12
使用systemctl status pgagent_12
查看服务的systemctl status pgagent_12
Configure it to auto-start, then start it, with:将其配置为自动启动,然后使用以下命令启动它:
sudo systemctl enable pgagent_12
sudo systemctl start pgagent_12
Most likely the authentication will fail, since the default .service file has身份验证很可能会失败,因为默认的 .service 文件具有
ExecStart=/usr/bin/pgagent_12 -s ${LOGFILE} hostaddr=${DBHOST} dbname=${DBNAME} user=${DBUSER} port=${DBPORT}
Confirm with sudo tail /var/log/pgagent_12.log
which will show用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]
To fix things, we need to create a .pgpass file that is accessible when the service starts.为了解决问题,我们需要创建一个在服务启动时可以访问的 .pgpass 文件。 First, stop the service一、停止服务
sudo systemctl stop pgagent_12
Examining the service file with less /usr/lib/systemd/system/pgagent_12.service
shows it has用less /usr/lib/systemd/system/pgagent_12.service
检查服务文件表明它有
User=pgagent
Group=pgagent
Furthermore, /etc/pgagent/pgagent_12.conf
has此外,/ /etc/pgagent/pgagent_12.conf
有
DBNAME=postgres
DBUSER=postgres
DBHOST=127.0.0.1
DBPORT=5432
LOGFILE=/var/log/pgagent_12.log
Examine the /etc/passwd
file to look for the pgagent user and its home directory: grep "pgagent" /etc/passwd
检查/etc/passwd
文件以查找 pgagent 用户及其主目录: grep "pgagent" /etc/passwd
pgagent:x:980:977:pgAgent Job Schedule:/home/pgagent:/bin/false
Thus, we need to create a .pgpass file at /home/pgagent/.pgpass
to define the postgres user's password因此,我们需要在/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
The directory and file permissions are important.目录和文件权限很重要。 If you're having problems, you can enable debug logging by editing the service file at /usr/lib/systemd/system/pgagent_12.service
to enable debug logging by updating the ExecStart
command to have -l 2
如果您遇到问题,您可以通过编辑位于/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}
After changing a .service file, things must be reloaded with sudo systemctl daemon-reload
(systemd will inform you of this requirement if you forget it).更改 .service 文件后,必须使用sudo systemctl daemon-reload
(如果您忘记了这个要求,systemd 会通知您)。
Keep starting/stopping the service and checking /var/log/pgagent_12.log
Eventually, it will start properly and sudo systemctl status pgagent_12
will show继续启动/停止服务并检查/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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.