Re: Systemd --user, block/wait until ready for commands from systemctl?

I've come up with a little better solution.

I can watch the $XDG_RUNTIME_DIR and wait for systemd to create the 'systemd/private' socket. Once it creates that socket it's ready for connections

So I can do

# If systemd --user isn't running then start it up
if ! systemctl --user &> /dev/null
then
# Double-fork the systemd process. This is done by executing a
# sub-shell (using ()) to ensure we're not the process leader, and
# then starting systemd using setsid to detach it from the
# controlling tty. Finally & to fork again to the background
(setsid /usr/lib/systemd/systemd --user --log-target=journal &)
# Wait for systemd socket directory to be created
inotifywait -t 3 -e create "$XDG_RUNTIME_DIR" &> /dev/null
fi

The downside to this is I had to install the inotify-tools package.

Still looking for any better suggestions.

Edit: I'm actually running into a race-condition with this, where systemd creates the socket file faster than inotifywait can start watching. If I check if the file exists first the race condition may still occur.