You are not logged in.

#1 2007-09-30 10:14:39

mienensuchkind
Member
Registered: 2007-01-21
Posts: 61

How to run a daemon at startup as a specific user?! [Solved]

Hi there, i am running btgd (bittorrent daemon) from the aur, and i have to start it manually after i logged in, because if i start it with my little daemon script (via rc.conf, copied from the mpd one), it gets startet as root, and i have problems with controling it as usual user, which quite sucks...

case "$1" in
  start)
    stat_busy "Starting Bittorrent Daemon"
    /usr/bin/btpd --bw-in 88 --bw-out 9 -d /home/jan/.btpd -p 6885 &> /dev/null

    if [ $? -gt 0 ]; then
      stat_fail
    else
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping Bittorrent Daemon"
    /usr/bin/btcli -d /home/jan/.btpd kill &> /dev/null
    if [ $? -gt 0 ]; then
      stat_fail
    else
      stat_done
    fi
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"
esac
exit 0

is it possible to run it as the user "jan" at boot, before i logged in?

Last edited by mienensuchkind (2007-09-30 10:45:13)

Offline

#2 2007-09-30 10:26:06

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: How to run a daemon at startup as a specific user?! [Solved]

Run it from ~/.bashrc? It will then be run when you log in, and you can put a test in there to see if it's already running so you don't run it twice.

EDIT: also possible: sudo -u jan -S your_passwd command

Last edited by Ramses de Norre (2007-09-30 10:28:28)

Offline

#3 2007-09-30 10:35:12

mienensuchkind
Member
Registered: 2007-01-21
Posts: 61

Re: How to run a daemon at startup as a specific user?! [Solved]

You know, i like your second one more because it was my intension to run it *without* me being logged in actually. i will instantly test it!

Thank you very much!

EDIT: Works! (you can forget the "-S password" thing, doesnt work with it, but does without!)

Thanks again!!

Last edited by mienensuchkind (2007-09-30 10:44:48)

Offline

#4 2007-09-30 10:52:42

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: How to run a daemon at startup as a specific user?! [Solved]

It works without given the password? Root is mighty it seems...

And I wrongly interpreted the man page, it should be echo your_passwd|sudo -u jan -S command but never mind if it works without wink

Offline

#5 2007-09-30 12:16:24

mienensuchkind
Member
Registered: 2007-01-21
Posts: 61

Re: How to run a daemon at startup as a specific user?! [Solved]

smile i was surprised myself aswell... but as you said, i dont mind as long as it works big_smile

Offline

#6 2008-02-17 12:59:41

axelgenus
Member
From: Italy
Registered: 2007-04-15
Posts: 100
Website

Re: How to run a daemon at startup as a specific user?! [Solved]

Try with this script:

#!/bin/bash

# general config
. /etc/rc.conf
. /etc/rc.d/functions

BTPD_BW_IN=600            # limit of incoming BitTorrent traffic [kB/s]
BTPD_BW_OUT=60            # limit of outgoing BitTorrent traffic [kB/s]
BTPD_USER=user            # user to run btpd
BTPD_DIR=/home/$BTPD_USER/.btpd    # directory to run btpd (default is $HOME/.btpd)
BTPD_PORT=6881            # port number to listen on

case "$1" in
  start)
    stat_busy "Starting BitTorrent Daemon"
    sudo -u $BTPD_USER /usr/bin/btpd --bw-in $BTPD_BW_IN --bw-out $BTPD_BW_OUT -d $BTPD_DIR -p $BTPD_PORT &> /dev/null

    if [ $? -gt 0 ]; then
      stat_fail
    else
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping BitTorrent Daemon"
    sudo -u $BTPD_USER /usr/bin/btcli -d $BTPD_DIR kill &> /dev/null
    if [ $? -gt 0 ]; then
      stat_fail
    else
      stat_done
    fi
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"
esac
exit 0

You have to create a new user and modify the script but it's safer and it behaves better with btcli.

Offline

Board footer

Powered by FluxBB