Skip to content

Instantly share code, notes, and snippets.

@spajak
Last active October 6, 2019 16:55
Show Gist options
  • Select an option

  • Save spajak/37bbf28c52f42406d2245c0a959074e4 to your computer and use it in GitHub Desktop.

Select an option

Save spajak/37bbf28c52f42406d2245c0a959074e4 to your computer and use it in GitHub Desktop.
My steps to enable systemd manager for any user (In this case www-data)
# Create user runtime directory
sudo install -d -o www-data -g www-data -m 0700 /run/user/`id -u www-data`
# Start user manager
sudo systemctl start user@`id -u www-data`
# Optionally enable lingering (User manager is spawned for the user at boot and kept around after logouts).
sudo loginctl enable-linger www-data
# Test it
sudo -u www-data \
XDG_RUNTIME_DIR=/run/user/`id -u www-data` \
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/`id -u www-data`/bus \
systemd-run --user echo "foo-`id -u`-bar"
# See if it's working
sudo systemctl status user@`id -u www-data`
# If not working try restarting this services:
sudo systemctl restart dbus.socket
sudo systemctl restart dbus.service
sudo systemctl restart user@`id -u www-data`
# Bonus: php-fpm
# If you want to be able execute a systemd task from a PHP script (using php-fpm)
# do this: in systemd unit file (mine is /lib/systemd/system/php-fpm.service) add:
[Service]
...
EnvironmentFile=/usr/local/etc/php/environment
# Create /usr/local/etc/php/environment with:
XDG_RUNTIME_DIR=/run/user/33
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/33/bus
# Replace 33 with `id -u www-data`
# Add this to /usr/local/etc/php/pool.d/www.conf near the end:
env[XDG_RUNTIME_DIR] = $XDG_RUNTIME_DIR
env[DBUS_SESSION_BUS_ADDRESS] = $DBUS_SESSION_BUS_ADDRESS
# Restart PHP FPM
sudo systemctl daemon-reload
sudo systemctl restart php-fpm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment