You are here: Big Brother Bot ForumAdd-OnsConfigurations (Moderator: MordyT)Fully Debian integrated start-stop script
Pages: [1] 2   Go Down
  Print  
Author Topic: Fully Debian integrated start-stop script  (Read 9054 times) Bookmark and Share
Senior Dev.
*
OS: Linux
Type: Home user
Posts: 3483
Offline Offline
WWW
Support Specialty: B3-Core, UrT/SmG/BFBC2 parsers, Plugin development
« on: March 25, 2009, 10:31:04 AM »

Here is a script that is meant to be placed in the /etc/init.d/ folder of debian.

It allows to start/stop a instance of B3.

script :
Quote
#! /bin/sh
### BEGIN INIT INFO
# Provides:          BigBrotherBot
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: BigBrotherBot
# Description:       BigBrotherBot
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DESC="BigBrotherBot"
NAME=b3
SCRIPTNAME=/etc/init.d/$NAME
DAEMON=`which python`
DAEMON_USER=place here the name of the user that is to be used to run b3
DAEMON_GROUP=place here the group of the user that is to be used to run b3

B3_BASEDIR=place here the path of the directory containing b3_run.py
B3_CONFIGFILE="place here the path of the b3.xml config file you want to use"

DAEMON_ARGS="$B3_BASEDIR/b3_run.py -c $B3_CONFIGFILE"

B3_PID_FILE="/var/run/bigbrotherbot/${NAME}.pid"


# Exit if the package is not installed
if [ ! -x "$DAEMON" ]; then
{
        echo "Couldn't find $DAEMON"
        exit 99
}
fi

PIDDIR=`dirname "$B3_PID_FILE"`
if [ "$PIDDIR" -a "$PIDDIR" != "/var/run" ] ; then
   if [ ! -d "$PIDDIR" ] ; then
      mkdir "$PIDDIR"
   fi
   if [ "$DAEMON_USER" ] ; then
       chown "$DAEMON_USER" "$PIDDIR"
   fi
   if [ "$DAEMON_GROUP" ] ; then
       chgrp "$DAEMON_GROUP" "$PIDDIR"
   fi
    fi
fi

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that check the daemon/service
#
do_status()
{
      NB_PROCESS=`ps ax | grep b3_run | grep "$B3_CONFIGFILE" | grep -v grep | wc -l`
      if [ $NB_PROCESS -gt 1 ]; then
         log_warning_msg "WARNING: $NAME is running multiple times"
            return 0
      elif [ $NB_PROCESS -eq 1 ]; then
         return 0
      else
            return 1
      fi
}

#
# Function that starts the daemon/service
#
do_start()
{
   # Return
   #   0 if daemon has been started
   #   1 if daemon was already running
   #   2 if daemon could not be started
   start-stop-daemon --start --quiet --chuid $DAEMON_USER:$DAEMON_GROUP --pidfile $B3_PID_FILE --make-pidfile \
        --background --chdir $B3_BASEDIR --startas $DAEMON -- $DAEMON_ARGS
    return $?
}

#
# Function that stops the daemon/service
#
do_stop()
{
   # Return
   #   0 if daemon has been stopped
   #   1 if daemon was already stopped
   #   2 if daemon could not be stopped
   #   other if a failure occurred
   start-stop-daemon --stop --signal 15 --retry 5 --quiet --pidfile $B3_PID_FILE
   RETVAL="$?"
    [ "$RETVAL" = 0 ] && rm -f $B3_PID_FILE
   [ "$RETVAL" = 2 ] && return 2
   return "$RETVAL"
}

do_kill() {
        while [ 1 ]; do
            PID=`ps hax | grep "b3_run" | grep "$B3_CONFIGFILE" | grep -v grep | cut -d' ' -f1 | head -n1`
            [ "$PID" = "" ] && return
            log_daemon_msg "killing process [$PID]"
            kill -9 $PID
        done
}

case "$1" in
  start)
   log_daemon_msg "Starting $DESC" "$NAME"
   do_start
   case "$?" in
      0|1) log_end_msg 0 ;;
      2)  log_end_msg 1 ;;
   esac
   ;;
  stop)
   log_daemon_msg "Stopping $DESC" "$NAME"
   do_stop
   case "$?" in
      0|1) log_end_msg 0 ;;
      2) log_end_msg 1 ;;
   esac
   ;;
  status)
   do_status
   case "$?" in
      1) log_success_msg " stopped" ;;
      0) log_success_msg " running" ;;
   esac
   ;;
  kill)
    log_daemon_msg "Killing all process b3_run using $B3_CONFIGFILE"
    do_kill
    log_end_msg 0
    ;;
  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
      ;;
     *)
        # Failed to stop
      log_end_msg 1
      ;;
   esac
   ;;
  *)
   echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
   exit 3
   ;;
esac

:


  • copy this script in /etc/init.d/b3
  • make it executable : chmod +x /etc/init.d/b3
  • now you can /etc/init.d/b3 start


Going further, auto starting b3 at system startup
just type :
Code:
update-rc.d b3 defaults

Going further with monit
add the following to your monit config file to teach monit how to look after your bot and restart it if it crashes.
Code:
check process b3 with pidfile /var/run/b3.pid
    start program = "/etc/init.d/b3 start"
    stop program  = "/etc/init.d/b3 stop"
    if 5 restarts within 5 cycles then timeout
« Last Edit: May 29, 2009, 09:22:04 AM by Courgette » Logged


Full Member
***
OS: Linux
Type: Owner dedicated server(s)
Gameservers: CoD4
Posts: 104
Offline Offline
WWW
« Reply #1 on: December 05, 2009, 12:23:45 PM »

THX  Cheesy
Comment
You have 1 fi too much Sad Line 47.


Your first step, for starters:
Explations
Go to the folder;
Make a new file named b3;
Go into a editor like vi, in this example nano;
Copy above code in Windows;
!Important just Right Mouse Click to past it into Nano editor;
Save it, press ctrl+x choose Y(es). Confirm by press the Enter key;
Set the good rights;

In Commands
cd /etc/init.d
sudo touch b3 (you can also skip this step, and just directly go to nano)
sudo nano b3
chmod +x b3


However its not working Sad
Quote
*user*@server:~$ sudo /etc/init.d/b3 start
 * Starting BigBrotherBot b3                                                                                                                          [ OK ]
*user*@server:~$ sudo /etc/init.d/b3 stop
 * Stopping BigBrotherBot b3                                                                                                                                 start-stop-daemon: warning: failed to kill 1502: No such process
                                                                        

And

Quote
*user*@server:~$ sudo update-rc.d b3 defaults
update-rc.d: warning: b3 stop runlevel arguments (0 1 6) do not match LSB Default-Stop values (S 0 1 6)
 Adding system startup for /etc/init.d/b3 ...
   /etc/rc0.d/K20b3 -> ../init.d/b3
   /etc/rc1.d/K20b3 -> ../init.d/b3
   /etc/rc6.d/K20b3 -> ../init.d/b3
   /etc/rc2.d/S20b3 -> ../init.d/b3
   /etc/rc3.d/S20b3 -> ../init.d/b3
   /etc/rc4.d/S20b3 -> ../init.d/b3
   /etc/rc5.d/S20b3 -> ../init.d/b3
« Last Edit: December 05, 2009, 01:17:02 PM by danger89 » Logged

JoinMyServer.com
Call of Duty 4 Game Servers
Senior Dev.
*
OS: Linux
Type: Home user
Posts: 3483
Offline Offline
WWW
Support Specialty: B3-Core, UrT/SmG/BFBC2 parsers, Plugin development
« Reply #2 on: December 05, 2009, 04:16:52 PM »

user*@server:~$ sudo /etc/init.d/b3 start
 * Starting BigBrotherBot b3                                                                                                                          [ OK ]
*user*@server:~$ sudo /etc/init.d/b3 stop
 * Stopping BigBrotherBot b3                                                                                                                                 start-stop-daemon: warning: failed to kill 1502: No such process
If your B3 config is incorrect and the bot fails to start up. Then it would make sence that the stop command find no process to kill. Can you make sure the bot was started ?
Logged

Full Member
***
OS: Linux
Type: Owner dedicated server(s)
Gameservers: CoD4
Posts: 104
Offline Offline
WWW
« Reply #3 on: December 05, 2009, 04:56:52 PM »

First of all, thank you for the fast support.

Second, I'm sure the config is completely right. Because the following is working:
Code:
python /home/*user*/b3/b3_run.py -c /home/*user*/b3/b3/conf/b3.xml
(NOT as root user)

That works, but I know 100% sure the bot doesn't start with the script above.

EDIT:
Please fix your code, remove line 47:
Code:
   fi

But its not working as a daemon  Undecided
I think here goes something wrong:
Code:
  start-stop-daemon --start --quiet --chuid $DAEMON_USER:$DAEMON_GROUP --pidfi$
        --background --chdir $B3_BASEDIR --startas $DAEMON -- $DAEMON_ARGS
    return $?

Maybe it will return a zero, but actually it's not started  Huh

Need it set quotes before & after the b3_basedir or something? Or must I change DAEMON=`which python` for example? Can I see somewhere a error log?
« Last Edit: December 05, 2009, 05:26:23 PM by danger89 » Logged

JoinMyServer.com
Call of Duty 4 Game Servers
Senior Dev.
*
OS: Linux
Type: Home user
Posts: 3483
Offline Offline
WWW
Support Specialty: B3-Core, UrT/SmG/BFBC2 parsers, Plugin development
« Reply #4 on: December 05, 2009, 05:39:53 PM »

to investigate further, can you start the script enabling bash debug mode :
Code:
bash -x /etc/init.d/b3 start
Then you will see if something looks weird with the variables values.
Logged

Full Member
***
OS: Linux
Type: Owner dedicated server(s)
Gameservers: CoD4
Posts: 104
Offline Offline
WWW
« Reply #5 on: December 05, 2009, 06:03:28 PM »

That returns this:
Code:
root@server:/home/*user*/b3/b3/conf# bash -x /etc/init.d/b3 start
+ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
+ DESC=BigBrotherBot
+ NAME=b3
+ SCRIPTNAME=/etc/init.d/b3
++ which python
+ DAEMON=/usr/bin/python
+ DAEMON_USER=*user*
+ DAEMON_GROUP=*user*
+ B3_BASEDIR=/home/*user*/b3
+ B3_CONFIGFILE=/home/*user*/b3/b3/conf/b3.xml
+ DAEMON_ARGS='/home/*user*/b3/b3_run.py -c /home/*user*/b3/b3/conf/b3.xml'
+ B3_PID_FILE=/var/run/bigbrotherbot/b3.pid
+ '[' '!' -x /usr/bin/python ']'
++ dirname /var/run/bigbrotherbot/b3.pid
+ PIDDIR=/var/run/bigbrotherbot
+ '[' /var/run/bigbrotherbot -a /var/run/bigbrotherbot '!=' /var/run ']'
+ '[' '!' -d /var/run/bigbrotherbot ']'
+ '[' *user* ']'
+ chown *user* /var/run/bigbrotherbot
+ '[' *user* ']'
+ chgrp *user* /var/run/bigbrotherbot
+ . /lib/lsb/init-functions
++ FANCYTTY=
++ '[' -e /etc/lsb-base-logging.sh ']'
++ . /etc/lsb-base-logging.sh
+ case "$1" in
+ log_daemon_msg 'Starting BigBrotherBot' b3
+ '[' -z 'Starting BigBrotherBot' ']'
+ log_use_usplash
+ '[' n = y ']'
+ type usplash_write
+ log_to_console log_daemon_msg 'Starting BigBrotherBot' b3
+ '[' n '!=' y ']'
+ '[' no '!=' yes ']'
++ readlink /proc/self/fd/0
+ stdin=/dev/pts/0
+ '[' /dev/pts/0 '!=' /dev/pts/0 ']'
+ return 0
+ log_use_fancy_output
+ TPUT=/usr/bin/tput
+ EXPR=/usr/bin/expr
+ '[' -t 1 ']'
+ '[' xxterm '!=' x ']'
+ '[' xxterm '!=' xdumb ']'
+ '[' -x /usr/bin/tput ']'
+ '[' -x /usr/bin/expr ']'
+ /usr/bin/tput hpa 60
+ /usr/bin/tput setaf 1
+ '[' -z ']'
+ FANCYTTY=1
+ case "$FANCYTTY" in
+ true
+ /usr/bin/tput xenl
++ /usr/bin/tput cols
+ COLS=157
+ '[' 157 ']'
+ '[' 157 -gt 6 ']'
++ /usr/bin/expr 157 - 7
+ COL=150
+ printf ' * Starting BigBrotherBot b3       '
 * Starting BigBrotherBot b3       ++ /usr/bin/expr 157 - 1
+ /usr/bin/tput hpa 156
                                                                                                                                                            + printf ' '
 + do_start
+ start-stop-daemon --start --quiet --chuid *user*:*user* --pidfile /var/run/bigbrotherbot/b3.pid --make-pidfile --background --chdir /home/*user*/b3 --startas /usr/bin/python -- /home/*user*/b3/b3_run.py -c /home/*user*/b3/b3/conf/b3.xml
+ return 0
+ case "$?" in
+ log_end_msg 0
+ '[' -z 0 ']'
+ log_use_usplash
+ '[' n = y ']'
+ type usplash_write
+ log_to_console log_end_msg 0
+ '[' n '!=' y ']'
+ '[' no '!=' yes ']'
++ readlink /proc/self/fd/0
+ stdin=/dev/pts/0
+ '[' /dev/pts/0 '!=' /dev/pts/0 ']'
+ return 0
+ '[' 150 ']'
+ '[' -x /usr/bin/tput ']'
+ printf '\r'
+ /usr/bin/tput hpa 150
                                                                                                                                                      + '[' 0 -eq 0 ']'
+ echo '[ OK ]'
[ OK ]
+ return 0
+ :

Its looks OK, right? Strange... Is the daemon auto-quit or destroy?

Quote
I changed my username to *user*.
« Last Edit: December 07, 2009, 01:50:45 PM by danger89 » Logged

JoinMyServer.com
Call of Duty 4 Game Servers
Senior Dev.
*
OS: Linux
Type: Home user
Posts: 3483
Offline Offline
WWW
Support Specialty: B3-Core, UrT/SmG/BFBC2 parsers, Plugin development
« Reply #6 on: December 05, 2009, 06:13:35 PM »

login as *user*, type :
Code:
cd /home/*user*/b3
/usr/bin/python /home/*user*/b3/b3_run.py -c /home/*user*/b3/b3/conf/b3.xml

If this works, then I have no clue why the script fails to do take the same actions...
Logged

Full Member
***
OS: Linux
Type: Owner dedicated server(s)
Gameservers: CoD4
Posts: 104
Offline Offline
WWW
« Reply #7 on: December 05, 2009, 06:22:07 PM »

OK thx, I will try that. Result:

Running  Smiley
After I deleted the log file, otherwise I get a premission denied.

------

And I also tried this:

Code:
start-stop-daemon -S -q -c melroy:melroy -p /var/run/bigbrotherbot/b3.pid -m -b -d /home/melroy/b3 -a /usr/bin/python /home/melroy/b3/b3_run.py -c /home/melroy/b3/b3/conf/b3.xml
(updated)

With as result:
Code:
start-stop-daemon: user `/home/*user*/b3/b3/conf/b3.xml' not found

BUT,
Code:
nano /home/*user*/b3/b3/conf/b3.xml
IS working...
« Last Edit: December 05, 2009, 06:43:39 PM by danger89 » Logged

JoinMyServer.com
Call of Duty 4 Game Servers
Senior Dev.
*
OS: Linux
Type: Home user
Posts: 3483
Offline Offline
WWW
Support Specialty: B3-Core, UrT/SmG/BFBC2 parsers, Plugin development
« Reply #8 on: December 05, 2009, 06:25:39 PM »

does your true username (the one you replaced with *user*) contains weird characters like blank characters ?
Logged

Full Member
***
OS: Linux
Type: Owner dedicated server(s)
Gameservers: CoD4
Posts: 104
Offline Offline
WWW
« Reply #9 on: December 05, 2009, 06:27:45 PM »

ok sorry its just:
melroy

That is my username


And this will result to "....b3.xml not found":
Code:
start-stop-daemon -S -q -c melroy:melroy -p /var/run/bigbrotherbot/b3.pid -m -b -c -d /home/melroy/b3 -a python /home/melroy/b3/b3_run.py  -c /home/melroy/b3/b3/conf/b3.xml
Logged

JoinMyServer.com
Call of Duty 4 Game Servers
Full Member
***
OS: Linux
Type: Owner dedicated server(s)
Gameservers: CoD4
Posts: 104
Offline Offline
WWW
« Reply #10 on: December 05, 2009, 06:31:07 PM »

I GOT IT!
You use -c at YOUR program  to get the config file. But.... start-stop-daemon uses also -c parameter!

Quote
  -c|--chuid <name|uid[:group|gid]>
                                change to this user/group before starting
                                  process
Logged

JoinMyServer.com
Call of Duty 4 Game Servers
Senior Dev.
*
OS: Linux
Type: Home user
Posts: 3483
Offline Offline
WWW
Support Specialty: B3-Core, UrT/SmG/BFBC2 parsers, Plugin development
« Reply #11 on: December 05, 2009, 06:32:20 PM »

There is something wrong with your test. You are using the "-c" option twice. Use the long options in place of the short ones for readability sake.
Code:
start-stop-daemon -S -q -c melroy:melroy -p /var/run/bigbrotherbot/b3.pid -m -b -c -d /home/melroy/b3 -a python /home/melroy/b3/b3_run.py  -c /home/melroy/b3/b3/conf/b3.xml

also, start-stop-daemon distinguishes its own parameters from the parameters of the program it has to start with the " -- " which are not in your test
« Last Edit: December 05, 2009, 06:34:35 PM by Courgette » Logged

Full Member
***
OS: Linux
Type: Owner dedicated server(s)
Gameservers: CoD4
Posts: 104
Offline Offline
WWW
« Reply #12 on: December 05, 2009, 06:34:05 PM »

Yep sorry. It make no differences same error, but again:

Quote
You use -c at YOUR program  to get the config file. But.... start-stop-daemon uses also -c parameter!

Quote
  -c|--chuid <name|uid[:group|gid]>
                                change to this user/group before starting
                                  process


 Roll Eyes

This works:
Code:
sudo start-stop-daemon -S -q -c melroy:melroy -p /var/run/bigbrotherbot/b3.pid -m -b -d /home/melroy/b3 -a /usr/bin/python /home/melroy/b3/b3_run.py
Aleast it's running in the background  Wink

Uhm I see I don't need the:
Code:
-c /home/melroy/b3/b3/conf/b3.xml
at the end.
But when I am going to run multiple server (I going to) I think I need to set the conf as parameter.
« Last Edit: December 05, 2009, 07:08:03 PM by danger89 » Logged

JoinMyServer.com
Call of Duty 4 Game Servers
Full Member
***
OS: Linux
Type: Owner dedicated server(s)
Gameservers: CoD4
Posts: 104
Offline Offline
WWW
« Reply #13 on: December 05, 2009, 07:08:21 PM »

UPDATE:  Grin
Find:
Code: shell
DAEMON_ARGS="$B3_BASEDIR/b3_run.py -c $B3_CONFIGFILE"

Change it to:
Code: shell
DAEMON_ARGS="$B3_BASEDIR/b3_run.py"

I hope there will be a solution fast.  Cool
Logged

JoinMyServer.com
Call of Duty 4 Game Servers
Full Member
***
OS: Linux
Type: Owner dedicated server(s)
Gameservers: CoD4
Posts: 104
Offline Offline
WWW
« Reply #14 on: December 07, 2009, 04:38:06 PM »

I don't What I did wrong, but the daemon looks like its working normal  Cheesy
I still think it as something to do with rights & that config file, I still doesn't know  Huh

But I want to update my rc.local again and I get this message:
Quote
sudo update-rc.d rc.local defaults
update-rc.d: warning: rc.local stop runlevel arguments (0 1 6) do not match LSB Default-Stop values (none)
 System start/stop links for /etc/init.d/rc.local already exist.

A warning? And I just want to make a start/stop link again to be sure :S?

EDIT:
I see you need for that last part monit, but its software what you need to install first:
sudo apt-get install monit

Beside that, I need to set startup to 1 in the file /etc/init.d/monit right ? It it correct if I add those 4 lines in /etc/monit/monitrc? Sorry if its sounds noobie  Embarrassed
« Last Edit: December 07, 2009, 04:51:43 PM by danger89 » Logged

JoinMyServer.com
Call of Duty 4 Game Servers
Tags:
Pages: [1] 2   Go Up
  Print  
 
Jump to:  


Rate this page +1 at Google Search


SimplePortal 2.3.1 © 2008-2009, SimplePortal