#!/bin/bash # Number of days to keep backup OAR files. DAYS="15" USER=$(whoami) console_name="OpenSim_console" screen_command="tmux -S /var/run/opensim/opensim-tmux.socket" screen_session=${console_name} screen_window="${screen_command} select-window -t ${screen_session}" osversion="current" bindir=/opt/opensim/${osversion}/bin quiet="" if [ $USER = "opensim" ] then SUDO="" else SUDO="sudo -Hu opensim" fi if [ "$1" = "-q" ] then quiet="true" shift 1 fi if [ "x$1" = "x" ]; then pathname=$(pwd) tgt=$(basename $pathname) elif [ -d "./$1" ]; then tgt=$1 elif [ -d "./sim$1" ]; then tgt=sim$1 fi if [ "x$tgt" = "x" ]; then echo "usage:" echo " $ $(basename $0) " echo "where is one of: " robust sim[0-9][0-9] exit 1 fi # Would be nice if this worked. export MONO_PATH=${bindir} # Then we would not have to do this, and subsequently write all over the damn bin directory. cd ${bindir} if ( ${screen_command} -q list-sessions 2>/dev/null | grep -q ${console_name}: ); then true else # The sudo is only so that the session is owned by opensim, otherwise it's owned by whoever ran this script, which is a likely security hole. # After the session is created, we rely on the /var/run/opensim directory to be group sticky, so that anyone in the opensim group can attach to the tmux socket. $SUDO ${screen_command} new-session -d -s ${console_name} fi inidir=/opt/opensim/config/${tgt} if [ "x$tgt" = "xrobust" ]; then exe="Robust" title="ROBUST" roboini="-inifile=${inidir}/Robust.ini" else exe="OpenSim" # Grab the first line of the sim.ini file, it should be the sim name in square brackets. title=$(head -n 1 ${inidir}/Regions/sim.ini ) # Strip off spaces at either end. shopt -s extglob title=${title##*( )} title=${title%%*( )} shopt -u extglob # Strip off the square brackets at either end. title=${title:1:$(( ${#title} - 2 ))} fi case $(basename $0) in "start-sim") cmd="/usr/bin/mono ${bindir}/${exe}.exe -inidirectory=${inidir} -logconfig=${inidir}/${exe}.exe.config ${roboini}" # Check if it's already running. if [ -e /var/run/opensim/${tgt}.pid ] then # Double check if it's REALLY running. if [ "x$(ps -p $(cat /var/run/opensim/${tgt}.pid) --no-headers -o comm)" = "x" ]; then $SUDO rm -f /var/run/opensim/${tgt}.pid fi fi # Now see if it's running. lol if [ ! -e /var/run/opensim/${tgt}.pid ] then ${screen_command} new-window -dn "[${title}]" -t "${screen_session}" "${cmd}" fi ;& "sim-console") if [ "x$quiet" = "x" ] then ${screen_window}:"[${title}]" \; attach-session -t "${screen_session}" fi ;; "backup-sim") # First delete old backups. Using mtime instead of ctime, so that people can touch files the want to keep, and coz ctime seems to have a bug. find "${inidir}/../../backups/" -name "${title// /_}-*.oar" -mtime +${DAYS} -type f -print0 | xargs -0 /bin/rm -f # Substitute underscores for spaces in the title, then add the full date and time to create the OAR file name. cmd="save oar ${inidir}/../../backups/${title// /_}-$(date '+%F_%T').oar" ${screen_window}:"[${title}]" \; send-keys "${cmd}" Enter ;; "stop-sim") ${screen_window}:"[${title}]" \; send-keys "shutdown" Enter ;; esac