0

I want to make a systemd unit for pgagnent.

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.

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?

krzysiexp
  • 11
  • 1
  • 1
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Sep 05 '18 at 00:37
  • could it be that it's not running as postgres user ? also, i don't think you need to pass hostaddr=localhost if you're going for unix socket and the postgres superuser. im keenly interested in your experience with this because i would also like to get pgagent running as a service this way :) – Reinsbrain Oct 12 '18 at 05:21

2 Answers2

3

If you installed pgagent with yum or apt-get, it should have created the systemd file for you. For example, on RHEL 7 (essentially CentOS 7), you can install PostgreSQL 12 followed by 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

In addition, it creates a systemd file at /usr/lib/systemd/system/pgagent_12.service

View the status of the service with 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

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

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

User=pgagent
Group=pgagent

Furthermore, /etc/pgagent/pgagent_12.conf has

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

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

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

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).

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

● 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.
Alchemistmatt
  • 383
  • 8
  • 12
1

systemd expects the ExecStart and ExecStop commands to include the full path to the executable.

start-stop-daemon is not necessary for services under systemd management. you will want to have it execute the underlying pgagent commands.

look at https://unix.stackexchange.com/questions/220362/systemd-postgresql-start-script for an example

  • Please avoid answering [off-topic](https://stackoverflow.com/help/on-topic) fodder. Instead, close it and move on. It is sometimes helpful to suggest a site where the question may be on-topic. – jww Sep 05 '18 at 00:37