我怎样才能使这个 deploy/init 以非 root 用户身份运行?

我怎样才能使这个 deploy/init 以非 root 用户身份运行?

我有一个需要以非 root 用户身份运行的 init 脚本。我该怎么做?Ubuntu 12.04

#!/bin/sh 
DEPLOY_BIN=/usr/share/deploy/bin
PID_FILE=/var/run/deploy.pid 
PATH=/bin:/usr/bin:/sbin:/usr/sbin:$DEPLOY_BIN DAEMON=$DEPLOY_BIN/reporting
NAME=deploy 
DESC="Deploy" if [ `id -u` -ne 0 ]; then
        echo "You need root privileges to run this script"
        exit 1 fi . /lib/lsb/init-functions if [ -r /etc/default/rcS ]; then
        . /etc/default/rcS fi case "$1" in
  start)
        log_daemon_msg "Starting $DESC"
        pid=`pidofproc -p $PID_FILE deploy`
        if [ -n "$pid" ] ; then
                log_begin_msg "Already running."
                log_end_msg 0
                exit 0
        fi
        # Start Daemon
        start-stop-daemon --start --pidfile "$PID_FILE" --make-pidfile --background --exec $DAEMON
        log_end_msg $?
        ;;
  stop)
        log_daemon_msg "Stopping $DESC"
        if [ -f "$PID_FILE" ]; then
                start-stop-daemon --stop --pidfile "$PID_FILE" \
                        --retry=TERM/20/KILL/5 >/dev/null
                if [ $? -eq 1 ]; then
                        log_progress_msg "$DESC is not running but pid file exists, cleaning up"
                elif [ $? -eq 3 ]; then
                        PID="`cat $PID_FILE`"
                        log_failure_msg "Failed to stop $DESC (pid $PID)"
                        exit 1
                fi
                rm -f "$PID_FILE"
        else
                log_progress_msg "(not running)"
        fi
        log_end_msg 0
        ;;
  status)
        status_of_proc -p $PID_FILE  deploy && exit 0 || exit $?
    ;;
  restart|force-reload)
        if [ -f "$PID_FILE" ]; then
                $0 stop
                sleep 1
        fi
        $0 start
        ;;
  *)
        log_success_msg "Usage: $0 {start|stop|restart|force-reload|status}"
        exit 1
        ;; esac exit 0

答案1

与流行的说法相反,su表示“切换用户”,而不是“超级用户”。所以

su user -c command 

(由 root 发出)应该可以工作。例如:

root@pern:~# su romano -c whoami
romano

相关内容