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 this startup script, when i launch i have the execution done by username: root, instead of username: sun.

How can i tell the start_daemon to execute it as username : sun not root?

$ ps aux | grep python
root       950  0.1  0.2 171132 18936 ?        S    05:35   0:00 /usr/bin/python /var/tmp/mp.py

root@nson:/etc/rc0.d# cat K20mp 
#! /bin/sh
PATH=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/var/tmp/mp.sh
PIDFILE=/var/run/mp.pid

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

case "$1" in
  start)
     log_daemon_msg "Starting mp"
     start_daemon -p $PIDFILE $DAEMON --user sun --chuid sun
     log_end_msg $?
   ;;
  stop)
     log_daemon_msg "Stopping mp"
     killproc -p $PIDFILE $DAEMON
     PID=`ps x |grep mp.py | head -1 | awk '{print $1}'`
     kill -9 $PID       
     log_end_msg $?
   ;;
  force-reload|restart)
     $0 stop
     $0 start
   ;;
  status)
     status_of_proc -p $PIDFILE $DAEMON atd && exit 0 || exit $?
   ;;
 *)
   echo "Usage: /etc/init.d/atd {start|stop|restart|force-reload|status}"
   exit 1
  ;;
esac
exit 0

EDIT: Reference not working

http://man.he.net/man8/start-stop-daemon

http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptfunc.html

share|improve this question
1  
which OS are you trying to run this on? –  Danila Ladner Feb 12 at 4:54
    
I am using: Ubuntu 13.10 64-bit (3.11.0-15-generic) –  YumYumYum Feb 12 at 4:58
1  
Doesn't the service start at all or does it start but run as root? Does the user sun exist (id sun)? –  Hauke Laging Feb 12 at 5:05
    
service starts fine. but the problem is it start as root. it needs to be started as username: sun (# id -u sun 1000 ) –  YumYumYum Feb 12 at 5:10
    
Can you paste from your OS "/etc/rc.d/init.d/functions" for me please? –  Danila Ladner Feb 12 at 5:18

1 Answer 1

up vote 1 down vote accepted

--user and --chuid works but has to be used with LINK

Example: As root user if now execute: /etc/init.d/PythonGUI start it execute as username: sun

#! /bin/sh
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Python GUI - Server"
NAME=PythonGUI.sh
DAEMON=/var/tmp/$NAME
DAEMON_ARGS="--options args"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/PythonGUI

[ -x "$DAEMON" ] || exit 0
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start() {
    start-stop-daemon --start --quiet --user sun --pidfile $PIDFILE --chuid sun --exec $DAEMON --test > /dev/null \
        || return 1
    start-stop-daemon --start --quiet --user sun --pidfile $PIDFILE --chuid sun --exec $DAEMON -- \
        $DAEMON_ARGS \
        || return 2
}

do_stop() {
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
    [ "$?" = 2 ] && return 2    
    rm -f $PIDFILE
    return "$RETVAL"
}

do_reload() {
    start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
    return 0
}

case "$1" in
  start)
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
    do_start
    case "$?" in
        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    ;;
  stop)
    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
    do_stop
    case "$?" in
        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    ;;
  status)
    status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;
  restart|force-reload)
    log_daemon_msg "Restarting $DESC" "$NAME"
    do_stop
    case "$?" in
      0|1)
        do_start
        case "$?" in
            0) log_end_msg 0 ;;
            1) log_end_msg 1 ;; # Old process is still running
            *) log_end_msg 1 ;; # Failed to start
        esac
        ;;
      *)
        log_end_msg 1
        ;;
    esac
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
    exit 3
    ;;
esac
:

EDIT:

update-rc.d apache2 start 20 2 3 4 . start 30 5 . stop 80 0 1 6 .

  • priority 20 (it can be 30, 80 etc)
  • runlevels for start / stop

Runlevels are logical groups of tasks. Traditionally you have five run levels.

0 boot
1 single user 
2 not used
3 multiuser
4 not used 
5 gui
6 reboot 
share|improve this answer

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.