1

I have a user account on Ubuntu Server 17.10 with user lingering enabled for systemd, which is, in my opinion, a great function. However, it's poorly documented. While I've managed to start up my service as this user manually using systemctl --user ... commands, I have a problem with dependencies.

In my ~/.config/systemd/user/foobar.service I have the dependency set this way:

[Unit]
Decription=StackExchange Foo Bar
After=network.target
# Some non-relevant stuff removed
[Install]
WantedBy=default.target

When I put posgresql.service it fails saying it doesn't know that one. I figured out in other answers, that lingered systemd doesn't see system-wide services. Only generic systemd targets, hence WantedBy should be set to default.target. But with that setting, the service (real application) itself fails to start on boot, as it cannot connect to the system-wide Postgresql [in time?].

So, the real question here is: How to setup all that in lingering mode, to have the working dependency on PosgreSQL at every boot? Any way to setup dummy postgreql.service that would link to the system one somehow? Are there any other methods? Any hint would be welcome.

EDIT: Postgresql is running as a system-wide application, not a service via a user account.

1

You define a target that requires PostgreSQL as follows:

$ cat postgresql.target
[Unit]
Description=Emergency Mode with Networking
Requires=network.target postgreql.service
After=postgreql.service
AllowIsolate=yes

This you add to your service:

[Unit]
Decription=StackExchange Foo Bar
After=network.target postgresql.target
# Some non-relevant stuff removed
[Install]
WantedBy=default.target

EDIT (After comment):

The above is incorrect, I assumed systemd in user-space could see any target.

After investigation, it appears to only know these targets:

default.target, shutdown.target, sockets.target, timers.target, paths.target, bluetooth.target, printer.target, smartcard.target, sound.target.

This means that you would have to add the postgresql.service to any of the above targets that makes sense, I would add it to sockets.target and have your foobar.service start AFTER default.target, which itself is reached AFTER sockets.target.

To add the postgresql.service to sockets.target, you add sockets.target to WantedBy= of postgresql.service, then update systemd config.

Another option would require netcat, nc -z <host> <port> will return 0 if the port is open ... in the script your service runs just loop while the PostgreSQL port is closed .... once the port opens, wait another second or two (or whatever is practical) and then have your service do what it is supposed to.

  • Good, that leads me somewhere. @thecarpy could you be so kind, and point correct location for the *.target files in the user's directory structure? – Michał F Dec 14 '17 at 13:54
  • Ups... I wasn't clear enough: PostgreSQL is a standard Ubuntu package running from system's systemd, my service is running from user's systemd. I'm starting to believe the whole lingering feature have a serious flaw, what effectively makes it more less useless without escaping to nasty tricks. There should be at least method to map user services & targets to system services & targets. – Michał F Dec 15 '17 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.