Sudo won't execute command as another user

Posted by TOdorus on Server Fault See other posts from Server Fault or by TOdorus
Published on 2012-10-18T13:37:27Z Indexed on 2012/10/18 17:05 UTC
Read the original article Hit count: 170

Filed under:
|
|
|

I'm trying to get a unicorn server to start when the server boots. I've created a shell script which works if I log as the ubuntu user and run

/etc/init.d/unicorn start

Shell script

#!/bin/sh

case "$1" in
start)
    cd /home/ubuntu/projects/asbest/current/
    unicorn_rails -c /home/ubuntu/projects/asbest/current/config/unicorn.rb -D -E production

;;
stop)
    if ps aux | awk '{print $2 }' | grep `cat ~/projects/asbest/current/tmp/pids/unicorn.pid`> /dev/null; then kill `cat ~/projects/asbest/current/tmp/pids/uni$
;;
restart)
    $0 stop
    $0 start
;;
esac

When I rebooted the server I noticed that the unicorn server wasn't listening to a socket. Since I ran the code succesfully as the ubuntu user I modified the script to let it always use the ubuntu user via sudo.

#!/bin/sh

case "$1" in
start)
    cd /home/ubuntu/projects/asbest/current/
    sudo -u ubuntu unicorn_rails -c /home/ubuntu/projects/asbest/current/config/unicorn.rb -D -E production
;;
stop)
    if ps aux | awk '{print $2 }' | grep `cat ~/projects/asbest/current/tmp/pids/unicorn.pid`> /dev/null; then sudo -u ubuntu kill `cat ~/projects/asbest/current/tmp/pids/uni$
;;
restart)
    $0 stop
    $0 start
;;
esac

After rebooting unicorn still wouldn't start, so I tried running the script from the command line. Now I get the following error

sudo: unicorn_rails: command not found

I've searched high and low to what could cause this, but I'm afraid I've tapped my limited understanding of Linux. From what I can understand is that although sudo should use the ubuntu user to execute the commands, it still uses the environment of the root user, which isn't configured to run ruby or unicorn. Does anybody have any experience with this?

© Server Fault or respective owner

Related posts about linux

Related posts about shell