You are not logged in.

#1 2011-07-21 11:48:13

duquesnc
Member
Registered: 2008-12-10
Posts: 94
Website

[solved] Writing an init rc script that switches to a given user

I want to write an rc script for plowbot ([github] [AUR]).

The script should switch to a given user, specified in /etc/conf.d/plowbot and launch plowbot. The problem is that I find myself unable to correctly log stdout and stderr. Ideally, one should be able to investigate crashes in logs. Since I would like to reuse the approach with different programs, I would enjoy if I could do this properly.

Here is my approach so far:

#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

DAEMON=plowbot
ARGS=

[ -r /etc/conf.d/$DAEMON ] && . /etc/conf.d/$DAEMON

PID=$(ps -o pid,cmd --ppid 1 | grep /usr/bin/$DAEMON | sed 's/ *\([0-9]\+\) .*/\1/')

case "$1" in
 start)
   stat_busy "Starting $DAEMON as user $DAEMON_USER"
   [ -z "$PID" ] && [ -n $DAEMON_USER ] && su -s '/bin/sh' -l $DAEMON_USER -c "/usr/bin/$DAEMON &"
   if [ $? = 0 ]; then
     add_daemon $DAEMON
     stat_done
   else
     stat_fail
     exit 1
   fi
   ;;
 stop)
   stat_busy "Stopping $DAEMON"
   [ -n "$PID" ] && kill $PID &>/dev/null
   if [ $? = 0 ]; then
     rm_daemon $DAEMON
     stat_done
   else
     stat_fail
     exit 1
   fi
   ;;
 restart)
   $0 stop
   sleep 1
   $0 start
   ;;
 *)
   echo "usage: $0 {start|stop|restart}"
esac

I have tried with different ways of appending '2>&1 | logger' in

su -s '/bin/sh' -l $DAEMON_USER -c "/usr/bin/$DAEMON &"

without any success.

Any idea?

Last edited by duquesnc (2011-07-22 08:34:31)

Offline

#2 2011-07-21 12:29:05

duquesnc
Member
Registered: 2008-12-10
Posts: 94
Website

Re: [solved] Writing an init rc script that switches to a given user

Nevermind, it's working (I don't know what was failing).

#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

DAEMON=plowbot
ARGS=

[ -r /etc/conf.d/$DAEMON ] && . /etc/conf.d/$DAEMON

PID=$(ps -o pid,cmd --ppid 1 | grep /usr/bin/$DAEMON | sed 's/ *\([0-9]\+\) .*/\1/')

case "$1" in
 start)
   stat_busy "Starting $DAEMON as user $DAEMON_USER"
   [ -z "$PID" ] && [ -n $DAEMON_USER ] && su -s '/bin/sh' -l $DAEMON_USER -c "/usr/bin/$DAEMON 2>&1 | logger &"
   if [ $? = 0 ]; then
     add_daemon $DAEMON
     stat_done
   else
     stat_fail
     exit 1
   fi
   ;;
 stop)
   stat_busy "Stopping $DAEMON"
   [ -n "$PID" ] && kill $PID &>/dev/null
   if [ $? = 0 ]; then
     rm_daemon $DAEMON
     stat_done
   else
     stat_fail
     exit 1
   fi
   ;;
 restart)
   $0 stop
   sleep 1
   $0 start
   ;;
 *)
   echo "usage: $0 {start|stop|restart}"
esac

Offline

#3 2011-07-22 08:49:29

duquesnc
Member
Registered: 2008-12-10
Posts: 94
Website

Re: [solved] Writing an init rc script that switches to a given user

By the way, in order to get properly the PID of the running daemon, I could not use pidof. That would be the program actually running is 'python2 /usr/bin/plowbot', not '/usr/bin/plowbot': to get the pid needed, I would have to run pidof python2 (but I would get more than one process).

Also, because of the user switch:

su -s '/bin/sh' -l $DAEMON_USER -c "/usr/bin/$DAEMON 2>&1 | logger &"

I could not use '$!' and store the pid in /var/run/plowbot.pid since in this case, '$!' would actually be related with su and not the command it launches.

So far, my workaround is to filter processes by parent pid 1 (that is what you get when you launch background processes with su) and to filter with the command argument (/usr/bin/plowbot). If anyone has a better subjection...

Offline

Board footer

Powered by FluxBB