Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have an init script to kick off a daemon. The problem is it runs as root. I would like it to run as a user called "deploy". Ubuntu 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
share|improve this question
2  
Change $DAEMON $DAEMON_OPTS to su - deploy -c "$DAEMON $DAEMON_OPTS" –  warl0ck Dec 1 '12 at 9:56
add comment

1 Answer 1

Use the start-stop-daemon utility to start your daemon. Pass the -c option to run it as a different user. You'll find some examples in /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."
    ;;
…
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.