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
CodePudding user response:
(This should be a comment rather than an answer, but I'm still building 'reputation' points, so...)
If Passenger has the same UID that myservice
needs to run, then that's not where the problem lies. Nor are the other ways of launching a child likely to change the outcome.
I suggest capturing the output of the subprocess as follows:
systemctl --user start myservice > /tmp/systemctl.output 2>&1
and then inspecting the output file after the subprocess fails.
CodePudding user response:
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