I need to start a systemd service from Ruby on Rails. My ruby code is using backticks to call systemctl:
`systemctl --user start myservice`
It's not working and I think it's to do with the user that command is executed by. Rails is running on Passenger and the user for that should be the same as my service.
I've also tried alternative ways to launch a subprocess
I was able to solve the issue by telling the shell where to find the correct DBUS for the user. I set the environment variable XDG_RUNTIME_DIR to the location of DBUS for the user, 1001 in my case. Check echo $UID
to find user id, I couldn't use this inline.
`export XDG_RUNTIME_DIR="/run/user/1001" && systemctl --user start myservice`
Thanks to D.j. Molny for the initial sign post and NeilCasey for the remainder