Start a process as a specific user
Solution 1
The sudo command is used to run things as other users, for example:
sudo -u rails ls $someDir
So I think the following should work (Try it and see):
sudo -u rails /usr/bin/god -c $GOD_CONFIG -P /var/run/god.pid -l /var/log/god.log
Also, check the sudo man page the god command depends on some environment variables being set...
Solution 2
Check the permissions on your script. They should be world readable and executable.
Also, in a couple of places in your script, you have:
usr/bin/god ...
It should probably be:
/usr/bin/god ...
Solution 3
This is generally accomplished in init scripts with su
:
su rails -c "/usr/bin/god -c $GOD_CONFIG -P /var/run/god.pid -l /var/log/god.log"
I haven't tested that, so I may have something weird wrong.
Also, assuming that it's been running as root so far, make sure that your pid and log files aren't owned by root and inaccessible to user rails.
Related videos on Youtube
Comments
-
JimNeath over 1 year
I have the following script which restarts god when I reboot my server (Ubuntu). How can I get god to start as the user "rails"?
#!/bin/sh ### BEGIN INIT INFO # Provides: god # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: God ### END INIT INFO NAME=god DESC=god PID=/home/rails/xxx/shared/pids/god.pid LOG=/home/rails/xxx/shared/log/god.log set -e # Make sure the binary and the config file are present before proceeding test -x /usr/bin/god || exit 0 # Create this file and put in a variable called GOD_CONFIG, pointing to # your God configuration file test -f /etc/default/god && . /etc/default/god [ $GOD_CONFIG ] || exit 0 . /lib/lsb/init-functions RETVAL=0 case "$1" in start) echo -n "Starting $DESC: " /bin/su rails -c '/usr/bin/god -c $GOD_CONFIG -P $PID -l $LOG' RETVAL=$? echo "$NAME." ;; stop) echo -n "Stopping $DESC: " kill `cat $PID` RETVAL=$? echo "$NAME." ;; restart) echo -n "Restarting $DESC: " kill `cat $PID` /bin/su rails -c '/usr/bin/god -c $GOD_CONFIG -P $PID -l $LOG' RETVAL=$? echo "$NAME." ;; status) /bin/su rails -c '/usr/bin/god status' RETVAL=$? ;; *) echo "Usage: god {start|stop|restart|status}" exit 1 ;; esac exit $RETVAL
Any help with this would be awesome, as I'm a total n00b when it comes to servers.
-
JimNeath over 14 yearsRunning the following from terminal gives the correct result: "/bin/su rails -c '/usr/bin/god -c /home/rails/xxx/current/config/god.rb -P /home/rails/xxx/shared/pids/god.pid -l /home/rails/xxx/shared/log/god.log'" Why won't this work in the script?
-
JimNeath over 14 yearsBoth the pid and log files are both owned by the rails user.
-
-
JimNeath over 14 yearsAlas, this didn't work. God failed to start at all. Thanks anyway :)
-
Kyle Brandt over 14 yearsDoes it fail silently? ... it should work, maybe env variables?
-
Kyle Brandt over 14 yearsIS the rails user able to read that config file?
-
Kyle Brandt over 14 yearsAlso, the rails user probably can't write the pid files in /var/run ...
-
JimNeath over 14 yearsI couldn't tell you the if it fails silently or not. I'm just rebooting the box and seeing if god comes back up, as the right user. As I said, I'm a n00b :)
-
JimNeath over 14 yearsAlso, I've tried "/bin/su rails -c 'god -c config/god.rb'" which works when I run it directly through terminal, but when I add it to the init.d script it fails to launch god at all
-
Kyle Brandt over 14 yearsI would try su rails, set the $GOD_CONFIG to the config file, and run that command in the script from the terminal and see what happens.
-
JimNeath over 14 yearsI originally copied the script from else where. I noticed the missing slashes when I pasted it before. Forgot to edit it. Thanks.
-
JimNeath over 14 yearsThe script is already world readable/executable. It works fine as it currently is. I just need to change it to start up at the rails user.