2
Donate to the B3 fund!

"even a small donation helps!"
Donate with PayPal!

Author Topic: start|stop|restart script for UBUNTU  (Read 16020 times)

Offline gtjj

  • Jr. Member
  • **
  • Posts: 27
start|stop|restart script for UBUNTU
« on: August 10, 2012, 11:45:43 AM »
1: Open Terminal
2: Write su and enter your password
3: Write the following command

Code: c++ [Select]
#: nano /etc/init.d/b3


4: Copy this script to created b3 file [change line 18 and 19 to yours]

--To COPY this script, press "Vieiw Plain" button and copy it

Code: javascript [Select]
#! /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
# Main-Date          2011.01.13
# Short-Description: BigBrotherBot Start-Stop-Restart
# Description:       BigBrotherBot
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SCRIPTNAME=/etc/init.d/$NAME
DAEMON=`which python`


B3_BASEDIR="/home/user/b3"
B3_CONFIGFILE="/home/user/b3/b3/conf/b3.xml"
B3_PID_FILE="/var/run/bigbrotherbot/${NAME}.pid"
DAEMON_ARGS="$B3_BASEDIR/b3_run.py -c $B3_CONFIGFILE"

# 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

# 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 "#! /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
# Main-Date          2011.01.13
# Short-Description: BigBrotherBot Start-Stop-Restart
# Description:       BigBrotherBot
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SCRIPTNAME=/etc/init.d/$NAME
DAEMON=`which python`


B3_BASEDIR="/home/user/b3"
B3_CONFIGFILE="/home/user/b3/b3/conf/b3.xml"
B3_PID_FILE="/var/run/bigbrotherbot/${NAME}.pid"
DAEMON_ARGS="$B3_BASEDIR/b3_run.py -c $B3_CONFIGFILE"

# 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

# 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

:" 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

:


5: Press CTRL+X -> Y -> ENTER
6: Write this code
Code: c++ [Select]
#: chmod +x /etc/init.d/b3

7: Run the console now!
Code: c++ [Select]
#: /etc/init.d/b3 start
#: /etc/init.d/b3 restart
#: /etc/init.d/b3 stop


Tested\Approved on Ubuntu 12.04
« Last Edit: February 19, 2013, 12:27:12 PM by Courgette »

Offline Tommy

  • Newbie
  • *
  • Posts: 3
Re: start|stop|restart script for UBUNTU
« Reply #1 on: February 18, 2013, 04:27:54 PM »
Hi!

I've installed B3 following this YouTube tutorial and everything worked fine.

Then I've made more instances of this B3, and of course I've changed paths and stuff, but here's the problem: I can't run two or more B3's at once. Each of these 9 B3's works fine, but when the first one is running I can't turn any other on. I don't know what's the problem, but maybe there's something with this start script. I don't understand much of the code of that start script but I believe that something must be changed there?

Thanks in advance,

Tommy
« Last Edit: February 18, 2013, 07:54:12 PM by Tommy »

Offline Courgette

  • Senior Dev.
  • Hero Member
  • *
  • Posts: 4883
    • Github repository
Re: start|stop|restart script for UBUNTU
« Reply #2 on: February 18, 2013, 11:04:36 PM »
with Ubuntu there is an easier way to define services : upstart

service for B3_1

create the file /etc/init/b3_1.conf with content :
Code: [Select]
description "BigBrotherBot service for server 1"

start on filesystem and net-device-up IFACE=lo

script
cd /home/me/b3
exec /usr/bin/python big-brother-bot/b3_run.py --conf /home/me/b3/conf/b3_1.xml --restart
end script
assuming you installed b3 in /home/me/b3 and that the b3 config file for the first server is /home/me/b3/conf/b3_1.xml.
Then type the following command :
Code: [Select]
sudo ln -s /lib/init/upstart-job /etc/init.d/b3_1


service for B3_2

create the file /etc/init/b3_2.conf with content :
Code: [Select]
description "BigBrotherBot service for server 2"

start on filesystem and net-device-up IFACE=lo

script
cd /home/me/b3
exec /usr/bin/python big-brother-bot/b3_run.py --conf /home/me/b3/conf/b3_2.xml --restart
end script
assuming you installed b3 in /home/me/b3 and that the b3 config file for the first server is /home/me/b3/conf/b3_2.xml.
Then type the following command :
Code: [Select]
sudo ln -s /lib/init/upstart-job /etc/init.d/b3_2

start / stop
With those two scripts, both B3 will be run at system startup (once the filesystem and network are ready).

You can stop the B3 services with :
Code: [Select]
sudo stop b3_1
sudo stop b3_2

You can start the B3 services with :
Code: [Select]
sudo start b3_1
sudo start b3_2

You can check the status of the B3 services with :
Code: [Select]
sudo status b3_1
sudo status b3_2

Offline Tommy

  • Newbie
  • *
  • Posts: 3
Re: start|stop|restart script for UBUNTU
« Reply #3 on: February 19, 2013, 12:25:40 AM »
Thanks for fast reply! I've tried to do as you said but it doesn't seem to work. There's nothing showing up on server console.

I tried this

Code: [Select]
sudo start b3_1
    b3_1 start/running, process 3352

sudo status b3_1
    b3_1 stop/waiting

I guess service isn't starting up. I've restarted computer and same problem. then I've checked services and got this

Code: [Select]
 [ ? ]  b3_1
 [ ? ]  b3_2
 [ ? ]  b3_3
 [ ? ]  b3_4
 [ ? ]  b3_5
 [ ? ]  b3_6
 [ ? ]  b3_7
 [ ? ]  b3_8
 [ ? ]  b3_9

Thanks for help,

Tommy

Offline Courgette

  • Senior Dev.
  • Hero Member
  • *
  • Posts: 4883
    • Github repository
Re: start|stop|restart script for UBUNTU
« Reply #4 on: February 19, 2013, 12:58:28 AM »
make sure that when you run
Code: [Select]
cd /home/me/b3
/usr/bin/python big-brother-bot/b3_run.py --conf /home/me/b3/conf/b3_1.xml --restart

it works correctly.

Only once you figured out that all is fine can you set it as a service.

Additionally you might want to redirect the script output to a log file to help assess issues :

Quote
description "BigBrotherBot service for server 2"

start on filesystem and net-device-up IFACE=lo

script
   cd /home/me/b3
   exec /usr/bin/python big-brother-bot/b3_run.py --conf /home/me/b3/conf/b3_2.xml --restart >> /var/log/b3/b3_2.log 2>&1
end script

« Last Edit: February 19, 2013, 01:04:02 AM by Courgette »

Offline Tommy

  • Newbie
  • *
  • Posts: 3
Re: start|stop|restart script for UBUNTU
« Reply #5 on: February 19, 2013, 09:46:08 AM »
Lawl I am truely sorry I've missed this

Code: [Select]
big-brother-bot/b3_run.py

should be just

Code: [Select]
b3_run.py

It's working now like a charm! Thanks!

Cheers,

Tommy

Offline djshylow

  • Jr. Member
  • **
  • Posts: 29
  • IIG
    • Immortal Inferno Gamers
Re: start|stop|restart script for UBUNTU
« Reply #6 on: January 04, 2015, 11:08:55 AM »
sudo /etc/init.d/b3_xbs start
/etc/init.d/b3_xbs: 33: /etc/init.d/b3_xbs: Syntax error: "then" unexpected (expecting "fi")

any solution ??

Offline Fenix

  • Dev. Team
  • Hero Member
  • *
  • Posts: 999
    • GitHub
Re: start|stop|restart script for UBUNTU
« Reply #7 on: January 04, 2015, 12:03:46 PM »
The syntax seems solid. Probably is an encoding problem. If you are running Windows on your computer, and Ubuntu on your server you probably have to convert the script to unix format:

  • sudo apt-get install dos2unix
  • sudo dos2unix /etc/init.d/b3_xbs start

However, you might want to upgrade your B3 to the latest dev build: http://files.cucurb.net/b3/daily/
I included a bash script which runs perfectly on Ubuntu: it provides functionalities to start/stop/restart B3 instance (also multiple ones) and it integrate nicely with in-game commands to stop and restart B3.
If you are interested you can find a brief tutorial here: https://github.com/BigBrotherBot/big-brother-bot/tree/release-1.10/scripts

Offline djshylow

  • Jr. Member
  • **
  • Posts: 29
  • IIG
    • Immortal Inferno Gamers
Re: start|stop|restart script for UBUNTU
« Reply #8 on: January 05, 2015, 03:54:13 AM »
 sudo dos2unix /etc/init.d/b3_xbs start
dos2unix: converting file /etc/init.d/b3_xbs to Unix format ...
dos2unix: start: No such file or directory
dos2unix: Skipping start, not a regular file.


is this wrong ?

screen -dm -S B3_28961 -t B3_28961 bash -c 'taskset -c 0 ./b3_run.py'
« Last Edit: January 05, 2015, 04:14:36 AM by djshylow »

Offline Fenix

  • Dev. Team
  • Hero Member
  • *
  • Posts: 999
    • GitHub
Re: start|stop|restart script for UBUNTU
« Reply #9 on: January 05, 2015, 12:14:47 PM »
sudo dos2unix /etc/init.d/b3_xbs start
dos2unix: converting file /etc/init.d/b3_xbs to Unix format ...
dos2unix: start: No such file or directory
dos2unix: Skipping start, not a regular file.

is this wrong ?
Yes, but probably because my copy&paste failed :P

You need to use dos2unix to convert the file to linux format, then use it normally, so:

This to convert the file to Unix format (use it just one time)
Code: [Select]
sudo dos2unix /etc/init.d/b3_xbs

Then start B3:

Code: [Select]
sudo /etc/init.d/b3_xbs start

Offline djshylow

  • Jr. Member
  • **
  • Posts: 29
  • IIG
    • Immortal Inferno Gamers
Re: start|stop|restart script for UBUNTU
« Reply #10 on: January 05, 2015, 03:44:01 PM »
I made a new file and still same error
 ???

[email protected]:~$ sudo dos2unix /etc/init.d/b3_IIG
dos2unix: converting file /etc/init.d/b3_IIG to Unix format ...
[email protected]:~$ sudo /etc/init.d/b3_xbs start
/etc/init.d/b3_xbs: 33: /etc/init.d/b3_xbs: Syntax error: "then" unexpected (expecting "fi")
[email protected]:~$

Same   :-\

 


Rate this page +1 at Google Search