Change font size
It is currently Wed Aug 21, 2019 7:19 pm

Forum rules


{L_IMAGE}
Please only post about software that directly relates to opensim, thank you.



Post a new topicPost a reply Page 1 of 1   [ 4 posts ]
Author Message
 Post subject: Start and Stop simulator
PostPosted: Mon Jul 22, 2019 8:01 am 

Joined: Thu Aug 16, 2018 6:41 am
Posts: 28
Scenario

I run OSgrid / OpenSimulator on a home hosted machine under user 'grid'. The main user account on that machine is 'media'. I wanted a way to connect to the machine via SSH mainly and start and stop the simulators that I have running, this is the result...

Requirements

Directory Structure

This requires at least the current version of the software (0e2abde as of the 22nd July 2019) and a previous version. This is so that a new release can be tested easily, and if there is a problem, you can drop back to your last working version.

You also need a seperate directory where all your simulators sit, in my case I use a directory called OVERRIDEosgrid which holds an override file, and a directory called Regions where the region configuration is held.
{L_CODE}:
-> /home
   -> 0e2abde
      -> bin
   -> 481d715
      -> bin
   -> OVERRIDEosgrid
      -> in1
         -> Regions
            - region.ini
         - override.ini
      -> in2
         -> Regions
            - region.ini
         - override.ini
      -> in3
         -> Regions
            - region.ini
         - override.ini
      -> in4
         -> Regions
            - region.ini
         - override.ini
      -> in5
         -> Regions
            - region.ini
         - override.ini

The minimum requirements for the override.ini are
{L_CODE}:
[Startup]
;; These are from OpenSim.ini
   PIDFile = "/home/grid/gridservers/osgrid-in1.pid"
   regionload_regionsdir = "/home/grid/gridservers/OVERRIDEosgrid/in1/Regions/"

[Network]
        http_listener_port = 9011

[DatabaseService]
        ;; These are from gridcommon.ini
        StorageProvider = "OpenSim.Data.MySQL.dll"
        ConnectionString = "Data Source=localhost;Database=YOUR_DATABASE_NAME;User ID=YOUR_DATABASE_USER;Password=YOUR_DATABASE_PASSWORD;"
.

You will need to alter PIDFile, regionload_regionsdir and http_listener_port for each of your instances.

The program
{L_CODE}:
#!/bin/bash
hostmachine=192.168.0.50
BaseDir="/home/grid/gridservers/OVERRIDEosgrid"
SimOwner="SimUserAccount"
OwnerPassword="SimOwnerPassword"
CURRENT_VERSION=0e2abde
PREVIOUS_VERSION=481d715
# NEW_VERSION=0e2abde


LoggedIn=`id -u -n`
declare -a filenamearray
declare -a overridenamearray
declare -a simname
declare -a simport
declare -a httpport
declare -a simstatus
if [ -z $DISPLAY ]
   then
      DIALOG=dialog
else
   # DIALOG=dialog
   DIALOG=gdialog
fi
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15
tempfile2=`subtempfile 2>/dev/null` || tempfile=/tmp/subtest$$
trap "rm -f $tempfile2" 0 1 2 5 15
cd $BaseDir
subdir=(`ls`)
subdirlen=${#subdir[*]}
inc1=0
inc2=0

function set_version {
   $DIALOG --colors --no-shadow --backtitle "Control Board" \
      --title "Version" --clear \
      --menu "Choose a version" 20 70 4 \
      "Current" "${CURRENT_VERSION}" \
      "Previous" "${PREVIOUS_VERSION}" \
      "Exit" "Exit the program" 2> $tempfile
   retval=$?
   choice=`cat $tempfile`
   case $retval in
      0)
         if [ "$choice" == "Current" ]
            then
               version=${CURRENT_VERSION}
               start_menu
         elif [ "$choice" == "Previous" ]
            then
               version=${PREVIOUS_VERSION}
               start_menu
         else
            main_menu
         fi
      ;;

                1)
         # Cancel chosen
         main_menu
                ;;

                255)
                        # ESC Chosen
         main_menu
                ;;
        esac
   trap "rm -f $tempfile" 0 1 2 5 15
   start_menu
}

function main_menu {
   sim_status
   $DIALOG --colors --no-shadow --backtitle "Control Board" \
      --title "Main Menu" --clear \
      --menu "Current Status \n${dialogstatus}\nPlease choose an option:" 20 70 4 \
      "Start" "Start an estate" \
      "Stop" "Close down an estate" \
      "Exit" "Exit the program" 2> $tempfile
   retval=$?
   choice=`cat $tempfile`
   case $retval in
      0)
         if [ "$choice" == "Start" ]
            then
               set_version
         elif [ "$choice" == "Stop" ]
            then
               stop_menu
         else
            clear
            exit 1
         fi
      ;;

      1)
         clear
         exit 1
      ;;

      255)
         clear
         exit 1
      ;;
   esac
}

function start_menu {
   sim_status
   inc1=0
   inc2=0
   simlist=()
   while [ $inc1 -lt $subdirlen ]; do
      simlist[$inc2]="${inc1}"
      let inc2++
      simlist[$inc2]="${simname[$inc1]}"
      let inc2++
      let inc1++
   done
   $DIALOG --no-tags --colors --no-shadow --backtitle "Control Board" \
      --title "Start Menu" --clear \
      --menu "Current Status \n${dialogstatus}\n\n Please choose an estate to start:" 25 70 7 \
      "${simlist[@]}" \
      "Back" "Main Menu" 2> $tempfile
   retval2=$?
   choice2=`cat $tempfile`
   case $retval2 in
      0)
         if [ "$choice2" == "Back" ]
            then
               main_menu
         fi
         screen_id="${subdir[$choice2]}"
         estate_name="${simname[$choice2]}"
         estate_port="${simport[$choice2]}"
         http_port="${httpport[$choice2]}"
         start_sim
      ;;

      1)
         # Cancel chosen.
         main_menu
         exit 1
      ;;

      255)
         # ESC chosen
         main menu
      ;;
   esac
   main_menu
}

function stop_menu {
   sim_status
   inc1=0
   inc2=0
   simlist=()
   while [ $inc1 -lt $subdirlen ]; do
      simlist[$inc2]="${inc1}"
      let inc2++
      simlist[$inc2]="${simname[$inc1]}"
      let inc2++
      let inc1++
   done
   $DIALOG --no-tags --colors --no-shadow --backtitle "Control Board" \
      --title "Stop Menu" --clear \
      --menu "Current Status \n${dialogstatus}\n\n Please choose an estate to stop:" 25 70 7 \
      "${simlist[@]}" \
      "Back" "Main Menu" 2> $tempfile
   retval2=$?
   choice2=`cat $tempfile`
   case $retval2 in
      0)
         if [ "$choice2" == "Back" ]
            then
               main_menu
         fi
         screen_id="${subdir[$choice2]}"
         estate_name="${simname[$choice2]}"
         estate_port="${simport[$choice2]}"
         http_port="${httpport[$choice2]}"
         stop_sim
      ;;

      1)
         # Cancel chosen.
         main_menu
      ;;

      255)
         # ESC chosen
         main_menu
      ;;
   esac
   main_menu
}

function start_sim {
   if (: < /dev/tcp/$hostmachine/$http_port) 2>/dev/null
      then
         $DIALOG --title "${estate_name}" \
            --msgbox "This region may already be running, please check and try again." 10 40
         start_menu
   else
      $DIALOG --title "${estate_name}" \
         --infobox "Starting ${estate_name} on screen ${screen_id}." 6 40
      screen -dmS $screen_id
      if [ "$LoggedIn" != "${SimOwner}" ]
         then
            screen -S $screen_id -X stuff 'su '$SimOwner'^M'`echo -ne '\015'`
            sleep 5
            screen -S $screen_id -X stuff $OwnerPassword'^M'`echo -ne '\015'`
      fi
      screen -S $screen_id -X stuff 'cd /home/grid/gridservers/'$version'/bin^M'`echo -ne '\015'`
      screen -S $screen_id -p 0 -X stuff 'mono OpenSim.exe -inidirectory='${BaseDir}'/'${screen_id}'/^M'`echo -ne '\015'`
      while ! nc -z ${hostmachine} ${http_port} </dev/null; do
         sleep 5
      done
      $DIALOG --title "${estate_name}" \
         --msgbox "${estate_name} has now started" 6 40
   fi
   start_menu
}

function stop_sim {
   if (: < /dev/tcp/$hostmachine/$http_port) 2>/dev/null
      then
         $DIALOG --title "${estate_name}" \
            --infobox "Shutdown of ${estate_name} in progress...\nPlease wait." 6 40
         screen -S $screen_id -X stuff 'shutdown^M'`echo -ne '\015'`
         until ! nc -z ${hostmachine} ${http_port} </dev/null; do
            sleep 10
         done
         screen -S $screen_id -X stuff 'exit^M'`echo -ne '\015'`
         sleep 10
         screen -S $screen_id -X stuff 'exit^M'`echo -ne '\015'`
         sleep 10
         stop_menu
   else
      $DIALOG --title "${estate_name}" \
         --msgbox "${estate_name} may not be running" 10 40
      stop_menu
   fi
}

function sim_status {
   inc1=0
   while [ $inc1 -lt $subdirlen ]; do
      if (: < /dev/tcp/${hostmachine}/${httpport[$inc1]}) 2>/dev/null
         then
            simstatus[$inc1]="Connection to \Zu${simname[$inc1]}\ZU \Zb\Z2succeeded\Zn on port ${simport[$inc1]} / ${httpport[$inc1]}"
      else
         simstatus[$inc1]="Connection to \Zu${simname[$inc1]}\ZU \Zb\Z1failed\ZB\Zn on port ${simport[$inc1]} / ${httpport[$inc1]}"
      fi
      let inc1++
   done
   inc1=0
   dialogstatus=${simstatus[$inc1]}
   dialogstatus+="\n"
   let inc1++
   while [ $inc1 -lt $subdirlen ]; do
      dialogstatus+=${simstatus[$inc1]}
      dialogstatus+="\n"
      let inc1++
   done
}

function get_dir {
   while [ $inc1 -lt $subdirlen ]; do
      cd $BaseDir/${subdir[$inc1]}/Regions
      files=(`ls *.ini`)
      filelen=${#files[*]}
      while [ $inc2 -lt $filelen ]; do
         filenamearray+=(${files[inc2]})
         let inc2++
      done
      inc2=0
      let inc1++
   done
   inc1=0
   while [ $inc1 -lt $subdirlen ]; do
      cd $BaseDir/${subdir[$inc1]}
                files=(`ls *.ini`)
                filelen=${#files[*]}
                while [ $inc2 -lt $filelen ]; do
                        overridenamearray+=(${files[inc2]})
                        let inc2++
                done
                inc2=0
                let inc1++
        done
   get_file
}

function get_file {
   while [ $inc2 -lt $subdirlen ]; do
      filepath=(${BaseDir}/${subdir[$inc2]}/Regions/${filenamearray[$inc2]})
      simport[$inc2]=$(awk '/InternalPort/ {print $3}' ${BaseDir}/${subdir[$inc2]}/Regions/${filenamearray[$inc2]})
      simname[$inc2]=`(head -n 1 $filepath | tail -n 1)`
      httpport[$inc2]=$(awk '/http_listener_port/ {print $3}' ${BaseDir}/${subdir[$inc2]}/${overridenamearray[$inc2]})
      tempstr="${simname[$inc2]:1}"
      simname[$inc2]=$tempstr
      tempstr="${simname[$inc2]:0:-1}"
      simname[$inc2]=$tempstr
      let inc2++
   done
}

get_dir
main_menu
cd ~
This may be overkill, but for me, it works. If you have a different way of doing this, or improvements to my code, please comment.

Regards
Baron Brown

(Edited 23 Jul 2019 to remove the requirement for the InternalPort to be on the second line and the http_listener_port to be InternalPort + 10)


Attachments:
File comment: Full program
grid.zip [1.97 KiB]
Downloaded 8 times
Top
 Profile  
 
 Post subject: Re: Start and Stop simulator
PostPosted: Sun Aug 18, 2019 2:50 am 
Site Admin
User avatar

Joined: Sun Jun 27, 2010 10:34 am
Posts: 156
Location: France
this bash script is well written but! it is too complicated and serves no purpose except complicate the lives of people!

_________________
Paela Argus,
Webmanagement and maintenance servers grid, Osgrid

https://www.osgrid.org


Top
 Profile  
 
 Post subject: Re: Start and Stop simulator
PostPosted: Sun Aug 18, 2019 12:57 pm 
Furious Typer

Joined: Tue Jan 31, 2012 12:40 pm
Posts: 96
This is the shell script I run as a cron job every 15 minutes:

#!/bin/bash
temp=`ps -ef | grep mono | grep -v grep`
if [[ -z "$temp" ]]
then
if [ -z "$STY" ]; then exec screen -dm -L -S myscreen /bin/bash "$0"; fi
cd $OPENSIM_PATH/bin
sudo ./opensim_sgen.sh
echo "OpenSim Started" >> $LOG_DIR/start_opensim.log
else
echo "OpenSim Running" >> $LOG_DIR/start_opensim.log
fi
export STY=

OPENSIM_PATH is the directory where you have installed Opensim; typically /opt/opensim
LOG_DIR is the directory you put your log files
opensim_sgen.sh is a shell script that executes mono -debug OpenSim.exe. I am running a mono compiled on the server machine, so the script contains a number of path mods to accommodate that.

The 15 minute delay is, of course, designed to give me time to react to server emergencies between re-boot and opensim starting.
To access the console after start up: sudo screen -r -d .


Top
 Profile  
 
 Post subject: Re: Start and Stop simulator
PostPosted: Tue Aug 20, 2019 4:34 am 

Joined: Thu Aug 16, 2018 6:41 am
Posts: 28
paela argus {L_WROTE}:
this bash script is well written
Thanks paela :)
paela argus {L_WROTE}:
but! it is too complicated and serves no purpose except complicate the lives of people!
:D I did say it might be overkill :D


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 4 posts ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
610nm Style by Daniel St. Jules of Gamexe.net