Daemon - how to tell the start daemon to execute this daemon as different username than root?
10,055
--user
and --chuid
works but has to be used with start-stop-daemon.
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
Related videos on Youtube
Author by
Admin
Updated on September 18, 2022Comments
-
Admin over 1 year
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
-
Danila Ladner over 10 yearswhich OS are you trying to run this on?
-
Hauke Laging over 10 yearsDoesn't the service start at all or does it start but run as
root
? Does the usersun
exist (id sun
)? -
Admin over 10 yearsservice starts fine. but the problem is it start as root. it needs to be started as username: sun (
# id -u sun 1000
) -
Danila Ladner over 10 yearsCan you paste from your OS "/etc/rc.d/init.d/functions" for me please?
-
Admin over 10 yearsIts working now see my Answer section. Thank you
-