1

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
  • 111
  • 1
  • 4
  • Running 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 Oct 02 '09 at 15:08
  • Both the pid and log files are both owned by the rails user. – JimNeath Oct 02 '09 at 15:09

3 Answers3

4

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...

Kyle Brandt
  • 83,619
  • 74
  • 305
  • 448
1

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 ...
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
  • I originally copied the script from else where. I noticed the missing slashes when I pasted it before. Forgot to edit it. Thanks. – JimNeath Oct 02 '09 at 13:48
  • The 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. – JimNeath Oct 02 '09 at 14:23
1

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.

wfaulk
  • 6,878
  • 7
  • 46
  • 75