以特定用户身份启动进程

我有以下脚本重新启动我的服务器(Ubuntu)重新启动神 。 我怎样才能让上帝成为用户的“铁轨”?

#!/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 

任何帮助,这将是非常棒的,因为我对服务器来说总共是n00b。

sudo命令用于以其他用户的身份运行,例如:

 sudo -u rails ls $someDir 

所以我认为以下应该工作(尝试看看):

 sudo -u rails /usr/bin/god -c $GOD_CONFIG -P /var/run/god.pid -l /var/log/god.log 

另外,请检查sudo手册页的上帝命令取决于一些环境variables正在设置…

检查您的脚本的权限。 他们应该是世界可读和可执行的。

另外,在脚本的几个地方,你有:

 usr/bin/god ... 

它可能应该是:

 /usr/bin/god ... 

这通常在su脚本中完成:

 su rails -c "/usr/bin/god -c $GOD_CONFIG -P /var/run/god.pid -l /var/log/god.log" 

我没有testing过,所以我可能有一些奇怪的错误。

另外,假设它已经以root用户身份运行,请确保您的pid和日志文件不是由root用户拥有的,并且不能被用户rails访问。