not sure why i've been running under the debug flag.
[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 # set up some parameters that we believe (or have been told) are beneficial.
6 export MONO_THREADS_PER_CPU=408
7
8 # we run the processes with a little cpu de-prioritization.  we do not want
9 # them taking over completely if there's a runaway mono tornado.
10 export NICENESS_LEVEL=6
11
12 # a tip that supposedly helps on linux so that there won't be bizarre
13 # problems compiling scripts.
14 export LANG=C
15
16 # this is used as a process startup snooze, to avoid running a dependent
17 # process before the dependency has really started.
18 export SNOOZE_TIME=7
19
20 # lock the limit in for threads, so we don't have any getting out of control.
21 # also make sure we've provided enough space for each thread.
22 ulimit -s 262144
23
24 # use more recent versions of mono for opensim if they're available.
25 if [ -d /opt/mono-2.10/bin ]; then
26   export PATH=/opt/mono-2.10/bin:$PATH
27 elif [ -d /opt/mono-2.8/bin ]; then
28   # use version 2.8 mono for opensim if it's available.
29   export PATH=/opt/mono-2.8/bin:$PATH
30 fi
31
32 function launch_screen()
33 {
34   screen_name="$1"; shift
35   app_name="$1"; shift
36   echo "$(mdate): starting $screen_name now..."
37   screen -L -S "$screen_name" -d -m nice -n $NICENESS_LEVEL mono "$app_name" 
38 ##why? --debug 
39 #-console=basic 
40   echo "$(mdate): $screen_name started."
41   sleep $SNOOZE_TIME
42 }
43
44 # finds the opensim process specified or returns a blank string in the
45 # OS_PROC_ID variable.
46 export OS_PROC_ID=
47 function find_opensim_process()
48 {
49   OS_PROC_ID=
50   process_name="$1"; shift
51   if [ -z "$process_name" ]; then
52     return 1  # failure in call.
53   fi
54   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)
55 }
56
57 # takes a screen name for the detached screen session and a process name that
58 # we should be able to find running.  we make sure that both are shut down.
59 function close_application()
60 {
61   screen_name="$1"; shift
62   process_name="$1"; shift
63   echo "$(mdate): stopping $screen_name now..."
64   screen -r -s "$screen_name" -X quit
65
66   # we don't want to shut any other servers down until this process is really gone.
67   find_opensim_process $process_name
68   if [ ! -z "$OS_PROC_ID" ]; then
69     echo "$(mdate): waiting for $screen_name to really shut down..."
70     sleep $SNOOZE_TIME
71     # check again after the snooze.
72     find_opensim_process $process_name
73     while [ ! -z "$OS_PROC_ID" ]; do
74       find_opensim_process $process_name
75 #break out on timed basis.
76     done
77     echo "$(mdate): $screen_name really is shut down now."
78
79 #do this as last ditch, above in timeout
80     find_opensim_process $process_name
81     if [ ! -z "$OS_PROC_ID" ]; then
82       echo "process for $screen_name still exists, killing $process_name (id $OS_PROC_ID) now."
83       kill -9 $OS_PROC_ID
84       sleep 2
85     fi
86
87   fi
88
89   echo "$(mdate): $screen_name stopped."
90 }
91