我有一个初始化脚本来启动守护进程。问题是它以 root 身份运行。我希望它以名为“deploy”的用户身份运行。乌班图12.04
#! /bin/sh
# File: /etc/init.d/unicorn
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the unicorn web server
# Description: starts unicorn
### END INIT INFO
DAEMON=/usr/local/bin/unicorn_rails
DAEMON_OPTS="-c /var/www/current/config/unicorn.rb -D"
NAME=unicorn
DESC="Unicorn"
PID=/var/www/current/shared/pid/unicorn.pid
case "$1" in
start)
echo -n "Starting $DESC: "
$DAEMON $DAEMON_OPTS
echo "$NAME."
;;
*)
echo "Usage: $NAME {start|stop|restart|reload}" >&2
exit 1
;;
esac
exit 0
答案1
使用start-stop-daemon
启动守护进程的实用程序。传递-c
(或--chuid
) 选项以其他用户身份运行它。您将在 中找到一些示例/etc/init.d/*
。
case $1 in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --chuid deploy --pidfile "$PID" --start --exec "$DAEMON" -- $DAEMON_OPTS
echo "$NAME."
;;
…
答案2
在 Ubuntu 上你可以使用
sudo -u deploy $DAEMON $DAEMON_OPTS