Merge branch 'release-2.140.109'
[feisty_meow.git] / scripts / opensim / opensim_utils.sh
1 #!/bin/bash
2 # this is a collection of scripts that assist in managing an opensim server.
3 # it uses the "screen" utility to manage opensimulator instances.
4
5 source "$FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh"
6
7 # set up some parameters that we believe (or have been told) are beneficial.
8 export MONO_THREADS_PER_CPU=1208
9
10 # we run the processes with a little cpu de-prioritization.  we do not want
11 # them taking over completely if there's a runaway mono tornado.
12 export NICENESS_LEVEL=6
13
14 # a tip that supposedly helps on linux so that there won't be bizarre
15 # problems compiling scripts.
16 export LANG=C
17
18 # this is used as a process startup snooze, to avoid running a dependent
19 # process before the dependency has really started.
20 export SNOOZE_TIME=6
21
22 # lock the limit in for threads, so we don't have any getting out of control.
23 # also make sure we've provided enough space for each thread.
24 ulimit -s 512144
25
26 # use more recent versions of mono for opensim if they're available.
27 if [ -d /opt/mono-2.10/bin ]; then
28   export PATH=/opt/mono-2.10/bin:$PATH
29 elif [ -d /opt/mono-2.8/bin ]; then
30   # use version 2.8 mono for opensim if it's available.
31   export PATH=/opt/mono-2.8/bin:$PATH
32 fi
33
34 function launch_screen()
35 {
36   screen_name="$1"; shift
37   app_name="$1"; shift
38   echo "$(date_stringer ' '): starting $screen_name now..."
39 #hmmm: version check for if we're using old screen?  this -L change was a mistake though for the screen project owners on ubuntu.
40 local boguslog=$HOME/screen_junk_$(date_stringer).log
41 #maybe they unbroke it in 17.10?  yes, but it requires NO space now.  *&@#*&@#
42 #hmmm: bring back old version but check for ubuntu 17.04 vs 17.10 now.
43 #actually they made it a new parm.  arghhh!
44   screen -L -S "$screen_name" -d -m nice -n $NICENESS_LEVEL mono "$app_name" 
45 #$boguslog 
46
47   echo "$(date_stringer ' '): $screen_name started."
48   # only sleep if we are not at the last process that gets started.
49   if [ "$app_name" != "OpenSim.exe" ]; then
50     sleep $SNOOZE_TIME
51   fi
52 }
53
54 # finds the opensim process specified or returns a blank string in the
55 # OS_PROC_ID variable.
56 export OS_PROC_ID=
57 function find_opensim_process()
58 {
59   OS_PROC_ID=
60   process_name="$1"; shift
61   if [ -z "$process_name" ]; then
62     return 1  # failure in call.
63   fi
64   OS_PROC_ID=$(ps wuax | grep "[0-9] mono $process_name" | grep -vi screen | sed -e "s/$USER  *\([0-9][0-9]*\).*/\1/" | head -n 1)
65 }
66
67 # takes a screen name for the detached screen session and a process name that
68 # we should be able to find running.  we make sure that both are shut down.
69 function close_application()
70 {
71   screen_name="$1"; shift
72   process_name="$1"; shift
73   echo "$(date_stringer ' '): stopping $screen_name now..."
74   screen -r -s "$screen_name" -X quit
75
76   # we don't want to shut any other servers down until this process is really gone.
77   find_opensim_process $process_name
78   if [ ! -z "$OS_PROC_ID" ]; then
79     echo "$(date_stringer ' '): waiting for $screen_name to really shut down..."
80     sleep $SNOOZE_TIME
81     # check again after the snooze.
82     find_opensim_process $process_name
83     while [ ! -z "$OS_PROC_ID" ]; do
84       find_opensim_process $process_name
85 #break out on timed basis.
86     done
87     echo "$(date_stringer ' '): $screen_name really is shut down now."
88
89 #do this as last ditch, above in timeout
90     find_opensim_process $process_name
91     if [ ! -z "$OS_PROC_ID" ]; then
92       echo "process for $screen_name still exists, killing $process_name (id $OS_PROC_ID) now."
93       kill -9 $OS_PROC_ID
94       sleep 2
95     fi
96
97   fi
98
99   echo "$(date_stringer ' '): $screen_name stopped."
100 }
101