Start a process as a specific user

6,201

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.

Share:
6,201

Related videos on Youtube

JimNeath
Author by

JimNeath

Ruby geek.

Updated on September 17, 2022

Comments

  • JimNeath
    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
      JimNeath over 14 years
      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
      JimNeath over 14 years
      Both the pid and log files are both owned by the rails user.
  • JimNeath
    JimNeath over 14 years
    Alas, this didn't work. God failed to start at all. Thanks anyway :)
  • Kyle Brandt
    Kyle Brandt over 14 years
    Does it fail silently? ... it should work, maybe env variables?
  • Kyle Brandt
    Kyle Brandt over 14 years
    IS the rails user able to read that config file?
  • Kyle Brandt
    Kyle Brandt over 14 years
    Also, the rails user probably can't write the pid files in /var/run ...
  • JimNeath
    JimNeath over 14 years
    I 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
    JimNeath over 14 years
    Also, 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
    Kyle Brandt over 14 years
    I 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
    JimNeath over 14 years
    I originally copied the script from else where. I noticed the missing slashes when I pasted it before. Forgot to edit it. Thanks.
  • JimNeath
    JimNeath over 14 years
    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.