Startup script linux

From OpenSimulator

(Difference between revisions)
Jump to: navigation, search
m
(added example systemd unit file for Rocky/RHEL 10 to run a stand-alone OpenSimulator)
 
(27 intermediate revisions by 11 users not shown)
Line 1: Line 1:
−
== /etc/init.d script for Linux  ==
+
{{Quicklinks|Startup_script_linux}}
  
−
=== Instructions ===
+
= Systemd Service Units for Linux =
 +
 
 +
== Systemd Service Unit (Unit File) for Rocky/RHEL 10 ==
 +
 
 +
Modern GNU/Linux enterprise distros have complex security mechanisms like selinux and require more involved configurations to securely run OpenSimulator. This is an example Unit File that will run a stand-alone OpenSimulator as a service via TMUX so that it can run in the background but still allow a system administrator to access the OpenSimulator console when needed. It will also issue a shutdown command to OpenSimulator when the service is stopped to ensure a clean shutdown.
 +
 
 +
'''Unit File - opensim.service'''
 +
<source lang="text">
 +
[Unit]
 +
Description=OpenSimulator Virtual World Server
 +
After=network.target
 +
 
 +
[Service]
 +
Type=simple
 +
User=opensim
 +
Group=opensim
 +
WorkingDirectory=/opt/opensim/bin
 +
 
 +
Environment=DOTNET_ROOT=/usr/lib64/dotnet
 +
Environment=HOME=/opt/opensim
 +
Environment=TMUX_TMPDIR=/tmp
 +
Environment=TERM=xterm-256color
 +
 
 +
# Start using script to get tty so we can maintain secure tmp and systemd active status
 +
ExecStart=/usr/bin/script -q -c "/usr/bin/tmux new-session -s opensim /usr/bin/dotnet OpenSim.dll -console=local" /dev/null
 +
 
 +
# Stop by sending opensim the shutdown command via tmux and waiting for process to end
 +
ExecStop=/bin/sh -c 'TMUX_TMPDIR=/tmp /usr/bin/tmux send-keys -t opensim "shutdown" Enter; while pgrep -u opensim -f "^/usr/bin/dotnet .*OpenSim\\.dll" >/dev/null 2>&1; do sleep 1; done'
 +
 
 +
# Opensim can take a while to safely shutdown, you may need to increase this value
 +
TimeoutStopSec=120
 +
 
 +
# Uncomment if you want systemd to automatically restart OpenSimulator
 +
#Restart=on-failure
 +
#RestartSec=15
 +
 
 +
LimitNOFILE=65536
 +
NoNewPrivileges=true
 +
ProtectSystem=full
 +
ProtectHome=true
 +
PrivateTmp=true
 +
 
 +
[Install]
 +
WantedBy=multi-user.target
 +
 
 +
</source>
 +
 
 +
'''Bash script to simplify console attachment - opensim-attach'''
 +
<source lang="text">
 +
#!/bin/bash
 +
#
 +
# This bash script will find the PID of the running simulator
 +
#  service and attach to the tmux session handling the console.
 +
#
 +
# To detach from the console without killing opensimulator, the
 +
# tmux command is Ctrl-b, d
 +
#
 +
sudo nsenter -t "$(systemctl show -p MainPID --value opensim.service)" -m --  runuser -u opensim -- env TMUX_TMPDIR=/tmp tmux attach -t opensim
 +
 
 +
</source>
 +
 
 +
'''Setup'''
 +
* Ensure tmux is installed
 +
  sudo dnf install tmux
 +
* Install opensim in /opt/opensim/bin
 +
* Create a user account for the OpenSimulator service that does not allow logins
 +
  sudo useradd --system --home-dir /opt/opensim --create-home --shell /usr/sbin/nologin opensim
 +
* Save the above unit file file as /etc/systemd/system/opensim.service
 +
* Save the above bash script as opensim-attach and make it executable
 +
'''Usage'''
 +
  systemctl enable opensim    # Enable the OpenSimulator service so it can be started and stopped
 +
  systemctl disable opensim  # Disable the OpenSimulator service
 +
  systemctl status opensim    # Check if the OpenSimulator service is running
 +
  systemctl start opensim    # Start the OpenSimulator service
 +
  systemctl stop opensim      # Stop the OpenSimulator service
 +
  systemctl restart opensim  # Restart the OpenSimulator service (stop + start)
 +
  journalctl -f -u opensim    # Tail the OpenSimulator service log output (script/tmux output and systemd error/warnings)
 +
  opensim-attach              # Attach to the OpenSimulator console (to detach the command is Ctrl-b, d )
 +
 
 +
 
 +
 
 +
== Systemd Service Unit / Unit File for CentOS/RHEL 7 and 8  ==
 +
 
 +
<source lang="text">
 +
# Systemd Service Unit for OpenSimulator
 +
# Author: steevithak
 +
#
 +
[Unit]
 +
Description=OpenSimulator service
 +
After=network.target
 +
 
 +
[Service]
 +
User=opensim
 +
Group=opensim
 +
Type=simple
 +
LimitSTACK=1048576
 +
TimeoutStopSec=60
 +
WorkingDirectory=/opt/opensim/bin
 +
ExecStart=/usr/bin/mono --server /opt/opensim/bin/OpenSim.exe
 +
 
 +
[Install]
 +
WantedBy=multi-user.target
 +
 
 +
</source>
 +
 
 +
'''Notes'''
 +
* Should work on CentOS 7.x, CentOS 8.x, RHEL 7.x, RHEL 8.x. Might work on other Redhat-based distros.
 +
* The "--server" option sets default garbage collection in mono which might be slightly faster for production usage. Using "--desktop" instead should also work and will set the garbage collection system to avoid expanding the heap as much as possible at the expense of slowing down garbage collection a bit.
 +
* There's a default max timeout of 60 seconds for shutdown. Adjust as needed if that's not long enough on your server.
 +
* This example doesn't use screen so the OpenSim console will not be available directly.
 +
 
 +
'''Setup'''
 +
* Create an opensim user (be sure to set shell to /sbin/nologin)
 +
* Install opensim in /opt/opensim
 +
* Save the above file as /etc/systemd/system/opensim.service
 +
'''Usage'''
 +
* systemctl enable opensim
 +
* systemctl disable opensim
 +
* systemctl status opensim
 +
* systemctl start opensim
 +
* systemctl stop opensim
 +
* systemctl restart opensim
 +
* journalctl -u opensim
 +
 
 +
= SysVinit Scripts for very old Linux Distros =
 +
 
 +
== Instructions ==
  
 
Tested on Debian 5. CHUIDS user to opensim user. Supports: start, stop, restart, status  
 
Tested on Debian 5. CHUIDS user to opensim user. Supports: start, stop, restart, status  
  
 
To implement: Type: cat &gt; /etc/init.d/opensim Paste in script from this page Press: CTRL+D Type: chmod +x /etc/init.d/opensim Script should now be located on server and be executable.  
 
To implement: Type: cat &gt; /etc/init.d/opensim Paste in script from this page Press: CTRL+D Type: chmod +x /etc/init.d/opensim Script should now be located on server and be executable.  
−
 
−
Type: update-rc.d opensim defaults Script should now be in your startup.
 
  
 
Modify header in script to adapt it to your installation folder and user.  
 
Modify header in script to adapt it to your installation folder and user.  
  
−
NOTE: stop-command currently doesn't work properly (it doesn't do a soft shutdown). It will kill OpenSim first with kill pid, then kill -9 pid. So make sure you have saved your data.&nbsp;:)
+
== Updated script ==
  
−
=== Script ===
+
Unlike Tedd's original script below, this relies on OpenSimulator's own PID functionality to work, so you must uncomment the PIDFile entries in the [Startup] section of OpenSim.ini/Robust.ini and set them to /tmp/OpenSim.exe.pid and /tmp/Robust.exe.pid as appropriate.
−
<pre>#! /bin/sh
+
 
 +
This allows us to simplify the script and get better error reporting. Cleanly shutting down OpenSimulator has also been implemented.
 +
 
 +
The delay between attempting clean shutdown and force shutting down has been doubled to 20 seconds.  However, in some cases this still might not be enough.  In the future, it would be better to poll for status.
 +
 
 +
This script is maintained at https://github.com/justincc/opensimulator-tools/blob/master/infrastructure/control/init.d/src/opensim  Pull requests welcome!
 +
 
 +
<source lang="bash">
 +
#! /bin/sh
 
### BEGIN INIT INFO
 
### BEGIN INIT INFO
−
# Provides: OpenSim
+
# Provides:         OpenSimulator
−
# Required-Start: $local_fs $network  
+
# Required-Start:   $local_fs $network  
−
# Required-Stop: $local_fs
+
# Required-Stop:     $local_fs
−
# Default-Start: 2 3 4 5
+
# Default-Start:     2 3 4 5
−
# Default-Stop: 0 1 6
+
# Default-Stop:     0 1 6
−
# Short-Description: Tedds OpenSim init.d-script
+
# Short-Description: Tedds OpenSimulator init.d-script, with further changes by Justin Clark-Casey (http://justincc.org)
 
### END INIT INFO
 
### END INIT INFO
−
# Put script in /etc/init.d/
 
−
# then execute: update-rc.d opensim defaults
 
  
 +
# Version 0.2.1
 +
# Put script in /etc/init.d/
 +
# Then execute /etc/init.d/opensim <start>|<stop>|<restart>|<status>
 +
#
 +
# You must configure Robust and OpenSim to create Robust.exe.pid and OpenSim.exe.pid PID files
 +
# by configuring PIDFile in the [Startup] section of OpenSim.ini and Robust.ini
 +
 
set -e
 
set -e
 +
 +
# Location of OpenSimulator binaries
 +
DIR=/opt/opensim/bin
  
−
#
+
# The directory where OpenSimulator and Robust are placing their pid files.  These must be of the form <service-name>.pid
−
# Directories
+
# e.g. OpenSim.exe.pid
−
#
+
PIDDIR=/tmp/
−
# Location of OpenSim installation
+
−
DIR=/home/opensim/OpenSim/bin/
+
# The user name which will execute the services
−
# Different PID dir?
+
−
PIDDIR=$DIR
+
−
 
+
 
USER=opensim
 
USER=opensim
−
SERVICES="OpenSim.Grid.UserServer.exe OpenSim.Server.exe OpenSim.exe"
 
  
 +
SERVICES="Robust.exe OpenSim.exe"
 +
#SERVICES="Robust.exe"
 +
#SERVICES="OpenSim.exe"
 +
 
#
 
#
 
# Kill values (in seconds)
 
# Kill values (in seconds)
Line 45: Line 185:
 
DELAY_STARTUP=10
 
DELAY_STARTUP=10
 
# How long between each service is sent shutdown command
 
# How long between each service is sent shutdown command
−
DELAY_KILL=10
+
DELAY_KILL=20
 
# After shutdown has been sent to all we do another loop with "kill", then "kill -9". How long between "kill" and "kill -9".
 
# After shutdown has been sent to all we do another loop with "kill", then "kill -9". How long between "kill" and "kill -9".
 
DELAY_FORCEKILL=10
 
DELAY_FORCEKILL=10
Line 51: Line 191:
 
#
 
#
 
# Info on service handled by this script
 
# Info on service handled by this script
−
#
+
 
# Name of service
 
# Name of service
 
NAME=opensim
 
NAME=opensim
 
# Description of service
 
# Description of service
−
DESC="OpenSim Server"
+
DESC="OpenSimulator Server"
−
 
+
 
# Binaries
 
# Binaries
 
SCREEN=/usr/bin/screen
 
SCREEN=/usr/bin/screen
 
MONO=/usr/bin/mono
 
MONO=/usr/bin/mono
−
 
+
 
###########################
 
###########################
 
##### START OF SCRIPT #####
 
##### START OF SCRIPT #####
 
###########################
 
###########################
−
 
+
 
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
 
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
−
 
+
−
&lt;br&gt;
+
−
 
+
 
# Load LSB log functions
 
# Load LSB log functions
 
_lsbFile=""
 
_lsbFile=""
 
if [ -e /etc/debian_version ]; then
 
if [ -e /etc/debian_version ]; then
−
_lsbFile="/lib/lsb/init-functions"
+
    _lsbFile="/lib/lsb/init-functions"
−
if [ -f $_lsbFile ]; then
+
    if [ -f $_lsbFile ]; then
−
. $_lsbFile
+
        . $_lsbFile
 +
    else
 +
        echo "This script requires LSB init-functions file which does not exist: $_lsbFile"
 +
        exit 1
 +
    fi
 
else
 
else
−
echo This script requires LSB init-functions file which doesn't exist: $_lsbFile
+
# [ -e /etc/init.d/functions ] ; then
−
exit 1
+
    _lsbFile="/etc/init.d/functions"
 +
    if [ -e $_lsbFile ]; then
 +
        . $_lsbFile
 +
    else
 +
        echo "This script requires LSB init-functions file which does not exist: $_lsbFile"
 +
        exit 1
 +
    fi
 
fi
 
fi
−
else
+
−
# [ -e /etc/init.d/functions ]&amp;nbsp;; then
+
−
_lsbFile="/etc/init.d/functions"
+
−
if [ -e $_lsbFile ]; then
+
−
. $_lsbFile
+
−
else
+
−
echo This script requires LSB init-functions file which doesn't exist: $_lsbFile
+
−
exit 1
+
−
fi
+
−
fi
+
−
 
+
 
# Lets use fancy output
 
# Lets use fancy output
 
log_use_fancy_output
 
log_use_fancy_output
−
 
+
 
# Check that target directory exists
 
# Check that target directory exists
−
if test&amp;nbsp;! -d "$DIR"; then
+
if test ! -d "$DIR"; then
−
log_failure_msg "$NAME" "Target directory \"$DIR\" does not exist. Can not continue."
+
    log_failure_msg "$NAME" "Target directory \"$DIR\" does not exist. Can not continue."
−
exit 1
+
    exit 1
 
fi
 
fi
−
 
+
 
# Create a reverse order for shutdown
 
# Create a reverse order for shutdown
 
SERVICES_REVERSE=""
 
SERVICES_REVERSE=""
 
reverse() { SERVICES_REVERSE="$9 $8 $7 $6 $5 $4 $3 $2 $1"; }
 
reverse() { SERVICES_REVERSE="$9 $8 $7 $6 $5 $4 $3 $2 $1"; }
 
reverse $SERVICES
 
reverse $SERVICES
−
 
+
 
# Check if a service is running
 
# Check if a service is running
 
isrunning() {  
 
isrunning() {  
−
ISRUNNING="0"
+
    ISRUNNING="0"
−
# Do we have PID-file?
+
    # Do we have PID-file?
−
if [ -f "$PIDDIR/$1.pid" ]; then
+
    if [ -f "$PIDDIR/$1.pid" ]; then
−
# Check if proc is running
+
        # Check if proc is running
−
pid=`cat "$PIDDIR/$1.pid" 2&amp;gt; /dev/null`
+
        pid=`cat "$PIDDIR/$1.pid" 2> /dev/null`
−
if [ "$pid"&amp;nbsp;!= "" ]; then
+
        if [ "$pid" != "" ]; then
−
if [ -d /proc/$pid ]; then
+
            if [ -d /proc/$pid ]; then
−
# Process is running
+
                # Process is running
−
ISRUNNING="1"
+
                ISRUNNING="1"
−
fi
+
            fi
−
fi
+
        fi
−
fi
+
    fi
−
#ISRUNNING="0"
+
    #ISRUNNING="0"
 
}
 
}
−
 
+
 
#
 
#
 
# Process commands
 
# Process commands
Line 126: Line 264:
 
case "$1" in
 
case "$1" in
 
start)
 
start)
−
# Start all services one by one
+
    # Start all services one by one
−
for server in $SERVICES; do
+
    for server in $SERVICES; do
−
log_daemon_msg "Starting $NAME" "$server"
+
        log_daemon_msg "Starting $NAME" "$server"
  
−
isrunning $server
+
        isrunning $server
−
case "$ISRUNNING" in
+
        case "$ISRUNNING" in
−
1) log_progress_msg "Process already started. Please stop first"; log_end_msg 1&amp;nbsp;;;
+
            1) log_daemon_msg "Process already started. Please stop first"; log_end_msg 1 ;;
−
0)  
+
            0)  
−
# Process is not running
+
                # Process is not running
−
# Start process and sleep...
+
                # Start process and sleep...
−
exefile="/tmp/exe.OpenSim.$server.sh"
+
                exefile="/tmp/exe.OpenSim.$server.sh"
  
−
# Need to make external file, had big problems with screen params
+
                # Need to make external file, had big problems with screen params
−
echo \#\!/bin/bash &amp;gt; $exefile
+
                echo \#\!/bin/bash > $exefile
−
echo cd $DIR &amp;gt;&amp;gt; $exefile
+
                echo cd $DIR >> $exefile
−
echo $MONO $server &amp;gt;&amp;gt; $exefile
+
                echo $MONO --debug $server >> $exefile
−
chmod +x $exefile
+
                chmod +x $exefile
  
−
cmd_screen="screen -S $server -d -m $exefile"
+
                cmd_screen="-S $server -d -m $exefile"
−
start-stop-daemon --start -b -x /bin/su -m --pidfile=/tmp/su.$server.pid --chdir "$DIR" -- - $USER "-c $cmd_screen"
+
                start-stop-daemon --start --pidfile $PIDDIR/$server.pid -u $USER --chdir $DIR --chuid $USER -x /usr/bin/screen -- $cmd_screen
  
−
# Delay between services that are started
+
                # Delay between services that are started
−
sleep $DELAY_STARTUP
+
                sleep $DELAY_STARTUP
  
−
rm $exefile 2&amp;gt; /dev/null
+
                rm $exefile 2> /dev/null
 +
 +
                isrunning $server
 +
                case "$ISRUNNING" in
 +
                    1)
 +
                        # Process started ok
 +
                        #log_daemon_msg "Success";
 +
                        log_end_msg 0
 +
                        ;;
 +
                    0)
 +
                        #log_daemon_msg "Failure";
 +
                        log_end_msg 1
 +
                        ;;
 +
                esac
 +
                ;;
 +
        esac
 +
    done
 +
    ;;
 +
 +
stop)
 +
    _killCount=0
  
−
# Make PID-file
+
    for server in $SERVICES_REVERSE; do
−
ps afxu | grep "mono $server" | grep -iEv "grep|screen" | awk {'print $2'} &amp;gt; "$PIDDIR/$server.pid"
+
        log_daemon_msg "Stopping $NAME" "$server"
  
−
isrunning $server
+
        isrunning $server
−
case "$ISRUNNING" in
+
        case "$ISRUNNING" in
−
1)  
+
        1)  
−
# Process started ok
+
            _killCount=`expr $_killCount + 1`
−
#log_progress_msg "Success";
+
            log_daemon_msg "Sending shutdown command:";  
−
log_end_msg 0
+
−
&amp;nbsp;;;
+
−
0)
+
−
# Process didn't start... remove pid-file
+
−
rm "$PIDDIR/$server.pid" 2&amp;gt; /dev/null
+
−
#log_progress_msg "Failure";  
+
−
log_end_msg 1
+
−
&amp;nbsp;;;
+
−
esac
+
−
&amp;nbsp;;;
+
−
esac
+
  
−
done
+
            cmd_screen="$SCREEN -S $server -p 0 -X stuff quit$(printf \\r)"
−
&amp;nbsp;;;
+
  
−
&lt;br&gt;
+
            # We can't use start-stop-daemon here currently because it will only send a signal to OpenSim
 +
            # --stop doesn't execute a command
 +
            # start-stop-daemon --stop --pidfile $PIDDIR/$server.pid -u $USER --chuid $USER -- $cmd_screen
  
−
stop)
+
            su $USER -c "$cmd_screen"
−
_killCount=0
+
  
−
for server in $SERVICES_REVERSE; do
+
            # Wait for it to shut down...
−
log_daemon_msg "Stopping $NAME" "$server"
+
            sleep $DELAY_KILL
  
−
isrunning $server
+
            isrunning $server
−
case "$ISRUNNING" in
+
            case "$ISRUNNING" in
−
1)  
+
                1) log_daemon_msg "$server is still running."; log_end_msg 0 ;;
−
_killCount=`expr $_killCount + 1`
+
                0) log_daemon_msg "$server has been shutdown"; log_end_msg 0 ;;
−
log_progress_msg "Sending shutdown command:";  
+
            esac
  
−
# We add some backspaces to the command in case something is on the console already
+
            ;;
−
echo `screen -S $server -X stuff \"  
+
        0)
 +
            log_daemon_msg "$server is not running"; log_end_msg 0
 +
            ;;
 +
        esac
  
−
# Wait for it to shut down...
+
    done
−
sleep $DELAY_KILL
+
  
−
isrunning $server
+
    # Check if any procs are still running
−
case "$ISRUNNING" in
+
    for server in $SERVICES; do
−
1) log_progress_msg "is still running."; log_end_msg 0&amp;nbsp;;;
+
        isrunning $server
−
0) log_progress_msg "has been shutdown"; log_end_msg 0&amp;nbsp;;;
+
        case "$ISRUNNING" in
−
esac
+
        1)  
 +
            log_warning_msg "Stopping $NAME" "$server is still running: Forcing kill"
 +
            echo `kill $pid 2> /dev/null`;
 +
            sleep $DELAY_FORCEKILL
 +
            echo `kill -9 $pid 2> /dev/null`;
 +
            sleep 1
  
−
&amp;nbsp;;;
+
            # Now check again if it is still running...
−
0)  
+
            isrunning $server
−
log_progress_msg "is not running"; log_end_msg 0
+
            case "$ISRUNNING" in
−
&amp;nbsp;;;
+
                0) log_daemon_msg "Success"; log_end_msg 0 ;;
−
esac
+
                1) log_daemon_msg "Process is still running... Even after \"kill -9 $pid\". WOW..."; log_end_msg 0 ;;
 +
            esac
 +
            ;;
 +
        0)
 +
            #log_daemon_msg "";
 +
            ;;
 +
        esac
 +
    done
  
−
done
+
    log_daemon_msg "$NAME: All done (stopped $_killCount processes)"; log_end_msg 0
  
−
# Check if any procs are still running
+
    ;;
−
for server in $SERVICES; do
+
−
isrunning $server
+
−
case "$ISRUNNING" in
+
−
1)
+
−
log_warning_msg "Stopping $NAME" "$server is still running: Forcing kill"
+
−
echo `kill $pid 2&amp;gt; /dev/null`;
+
−
sleep $DELAY_FORCEKILL
+
−
echo `kill -9 $pid 2&amp;gt; /dev/null`;
+
−
sleep 1
+
  
−
# Now check again if it is still running...
+
status)
−
isrunning $server
+
    # Count how many processes we need
−
case "$ISRUNNING" in
+
    PROCCOUNT=0
−
0) log_progress_msg "Success"; log_end_msg 0&amp;nbsp;;;
+
    for i in $SERVICES; do
−
1) log_progress_msg "Process is still running... Even after \"kill -9 $pid\". WOW..."; log_end_msg 0&amp;nbsp;;;
+
        PROCCOUNT=`expr $PROCCOUNT + 1`
 +
    done
 +
 
 +
    # Go through server PID files and count how many are running
 +
    log_daemon_msg "$NAME: Running processes:"
 +
    _pidCount=0
 +
    for server in $SERVICES; do
 +
 
 +
        isrunning $server
 +
        case "$ISRUNNING" in
 +
            1)  
 +
                # This server is running
 +
                _pidCount=`expr $_pidCount + 1`
 +
                log_daemon_msg "$server"
 +
                ;;
 +
            0)
 +
                ;;
 +
        esac
 +
    done
 +
 
 +
    log_daemon_msg " ($_pidCount of $PROCCOUNT processes are running)"
 +
 
 +
    # Check if running proc count matches requires proc count
 +
    if [ $_pidCount -eq $PROCCOUNT ]; then
 +
        log_daemon_msg "$NAME is running"
 +
        exit 0
 +
    else
 +
        log_daemon_msg "$NAME is NOT running"
 +
        exit 1
 +
    fi
 +
    ;;
 +
 
 +
restart)
 +
    $0 stop
 +
    $0 start
 +
    ;;
 +
*)
 +
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}" >&2
 +
    exit 1
 +
    ;;
 
esac
 
esac
−
&amp;nbsp;;;
 
−
0)
 
−
#log_progress_msg "";
 
−
&amp;nbsp;;;
 
−
esac
 
−
done
 
  
−
log_begin_msg "$NAME: All done (killed $_killCount procs)"; log_end_msg 0
+
exit 0
 +
</source>
  
−
&amp;nbsp;;;
+
== Tedd's Original Script plus clean shutdown tweak ==
  
−
&lt;br&gt;
+
<source lang="bash">
  
−
&lt;br&gt;
+
#! /bin/sh
 +
### BEGIN INIT INFO
 +
# Provides:          OpenSimulator
 +
# Required-Start:    $local_fs $network
 +
# Required-Stop:    $local_fs
 +
# Default-Start:    2 3 4 5
 +
# Default-Stop:      0 1 6
 +
# Short-Description: Tedds OpenSimulator init.d-script
 +
### END INIT INFO
 +
 +
# Put script in /etc/init.d/
 +
# then execute: update-rc.d opensim defaults
 +
 +
set -e
 +
 +
#
 +
# Directories
 +
#
 +
# Location of OpenSimulator installation
 +
DIR=/home/opensim/OpenSim/bin/
 +
# Different PID dir?
 +
PIDDIR=$DIR
 +
 +
USER=opensim
 +
SERVICES="Robust.exe OpenSim.exe"
 +
 +
#
 +
# Kill values (in seconds)
 +
#
 +
# How long between each service being started
 +
DELAY_STARTUP=10
 +
# How long between each service is sent shutdown command
 +
DELAY_KILL=10
 +
# After shutdown has been sent to all we do another loop with "kill", then "kill -9". How long between "kill" and "kill -9".
 +
DELAY_FORCEKILL=10
 +
 +
#
 +
# Info on service handled by this script
 +
 +
# Name of service
 +
NAME=opensim
 +
# Description of service
 +
DESC="OpenSimulator Server"
 +
 +
# Binaries
 +
SCREEN=/usr/bin/screen
 +
MONO=/usr/bin/mono
 +
 +
###########################
 +
##### START OF SCRIPT #####
 +
###########################
 +
 +
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
 +
 +
 
 +
 +
# Load LSB log functions
 +
_lsbFile=""
 +
if [ -e /etc/debian_version ]; then
 +
    _lsbFile="/lib/lsb/init-functions"
 +
    if [ -f $_lsbFile ]; then
 +
        . $_lsbFile
 +
    else
 +
        echo "This script requires LSB init-functions file which does not exist: $_lsbFile"
 +
        exit 1
 +
    fi
 +
else
 +
# [ -e /etc/init.d/functions ] ; then
 +
    _lsbFile="/etc/init.d/functions"
 +
    if [ -e $_lsbFile ]; then
 +
        . $_lsbFile
 +
    else
 +
        echo "This script requires LSB init-functions file which does not exist: $_lsbFile"
 +
        exit 1
 +
    fi
 +
fi
 +
 +
# Lets use fancy output
 +
log_use_fancy_output
 +
 +
# Check that target directory exists
 +
if test ! -d "$DIR"; then
 +
    log_failure_msg "$NAME" "Target directory \"$DIR\" does not exist. Can not continue."
 +
    exit 1
 +
fi
 +
 +
# Create a reverse order for shutdown
 +
SERVICES_REVERSE=""
 +
reverse() { SERVICES_REVERSE="$9 $8 $7 $6 $5 $4 $3 $2 $1"; }
 +
reverse $SERVICES
 +
 +
# Check if a service is running
 +
isrunning() {
 +
    ISRUNNING="0"
 +
    # Do we have PID-file?
 +
    if [ -f "$PIDDIR/$1.pid" ]; then
 +
        # Check if proc is running
 +
        pid=`cat "$PIDDIR/$1.pid" 2> /dev/null`
 +
        if [ "$pid" != "" ]; then
 +
            if [ -d /proc/$pid ]; then
 +
                # Process is running
 +
                ISRUNNING="1"
 +
            fi
 +
        fi
 +
    fi
 +
    #ISRUNNING="0"
 +
}
 +
 +
#
 +
# Process commands
 +
#
 +
case "$1" in
 +
start)
 +
    # Start all services one by one
 +
    for server in $SERVICES; do
 +
        log_daemon_msg "Starting $NAME" "$server"
 +
 +
        isrunning $server
 +
        case "$ISRUNNING" in
 +
            1) log_progress_msg "Process already started. Please stop first"; log_end_msg 1 ;;
 +
            0)
 +
                # Process is not running
 +
                # Start process and sleep...
 +
                exefile="/tmp/exe.OpenSim.$server.sh"
 +
 +
                # Need to make external file, had big problems with screen params
 +
                echo \#\!/bin/bash > $exefile
 +
                echo cd $DIR >> $exefile
 +
                echo $MONO $server >> $exefile
 +
                chmod +x $exefile
 +
 +
                cmd_screen="screen -S $server -d -m $exefile"
 +
                start-stop-daemon --start -b -x /bin/su -m --pidfile=/tmp/su.$server.pid --chdir "$DIR" -- - $USER "-c $cmd_screen"
 +
 +
                # Delay between services that are started
 +
                sleep $DELAY_STARTUP
 +
 +
                rm $exefile 2> /dev/null
 +
 +
                # Make PID-file
 +
                ps afxu | grep "mono $server" | grep -iEv "grep|screen" | awk {'print $2'} > "$PIDDIR/$server.pid"
 +
 +
                isrunning $server
 +
                case "$ISRUNNING" in
 +
                    1)
 +
                        # Process started ok
 +
                        #log_progress_msg "Success";
 +
                        log_end_msg 0
 +
                        ;;
 +
                    0)
 +
                        # Process did not start... remove pid-file
 +
                        rm "$PIDDIR/$server.pid" 2> /dev/null
 +
                        #log_progress_msg "Failure";
 +
                        log_end_msg 1
 +
                        ;;
 +
                esac
 +
                ;;
 +
        esac
 +
 +
    done
 +
    ;;
 +
 +
 
 +
 +
stop)
 +
    _killCount=0
 +
 +
    for server in $SERVICES_REVERSE; do
 +
        log_daemon_msg "Stopping $NAME" "$server"
 +
 +
        isrunning $server
 +
        case "$ISRUNNING" in
 +
        1)
 +
            _killCount=`expr $_killCount + 1`
 +
            log_progress_msg "Sending shutdown command:";
 +
 +
            cmd_screen="screen -S $server -p 0 -X stuff quit$(printf \\r)"
 +
            su $USER -c "$cmd_screen"
 +
 +
            # Wait for it to shut down...
 +
            sleep $DELAY_KILL
 +
 +
            isrunning $server
 +
            case "$ISRUNNING" in
 +
                1) log_progress_msg "is still running."; log_end_msg 0 ;;
 +
                0) log_progress_msg "has been shutdown"; log_end_msg 0 ;;
 +
            esac
 +
 +
            ;;
 +
        0)
 +
            log_progress_msg "is not running"; log_end_msg 0
 +
            ;;
 +
        esac
 +
 +
    done
 +
 +
    # Check if any procs are still running
 +
    for server in $SERVICES; do
 +
        isrunning $server
 +
        case "$ISRUNNING" in
 +
        1)
 +
            log_warning_msg "Stopping $NAME" "$server is still running: Forcing kill"
 +
            echo `kill $pid 2> /dev/null`;
 +
            sleep $DELAY_FORCEKILL
 +
            echo `kill -9 $pid 2> /dev/null`;
 +
            sleep 1
 +
 +
            # Now check again if it is still running...
 +
            isrunning $server
 +
            case "$ISRUNNING" in
 +
                0) log_progress_msg "Success"; log_end_msg 0 ;;
 +
                1) log_progress_msg "Process is still running... Even after \"kill -9 $pid\". WOW..."; log_end_msg 0 ;;
 +
            esac
 +
            ;;
 +
        0)
 +
            #log_progress_msg "";
 +
            ;;
 +
        esac
 +
    done
 +
 +
    log_begin_msg "$NAME: All done (killed $_killCount procs)"; log_end_msg 0
 +
 +
    ;;
 +
 +
 
 +
 +
 
 +
 +
status)
 +
    # Count how many processes we need
 +
    PROCCOUNT=0
 +
    for i in $SERVICES; do
 +
        PROCCOUNT=`expr $PROCCOUNT + 1`
 +
    done
 +
 +
    # Go through server PID files and count how many are running
 +
    log_begin_msg "$NAME: Running processed:"
 +
    _pidCount=0
 +
    for server in $SERVICES; do
 +
 +
        isrunning $server
 +
        case "$ISRUNNING" in
 +
            1)
 +
                # This server is running
 +
                _pidCount=`expr $_pidCount + 1`
 +
                log_progress_msg "$server"
 +
                ;;
 +
            0)
 +
                ;;
 +
        esac
 +
    done
 +
 +
    log_begin_msg " ($_pidCount of $PROCCOUNT processes are running)"; log_end_msg 0
 +
 +
    # Check if running proc count matches requires proc count
 +
    log_begin_msg "$NAME is"
 +
    if [ $_pidCount -eq $PROCCOUNT ]; then
 +
        log_progress_msg "running"; log_end_msg 0
 +
        exit 0
 +
    else
 +
        log_progress_msg "NOT running"; log_end_msg 0
 +
        exit 1
 +
    fi
 +
    ;;
 +
 +
restart)
 +
    $0 stop
 +
    $0 start
 +
    ;;
 +
*)
 +
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}" >&2
 +
    exit 1
 +
    ;;
 +
esac
 +
 +
exit 0
 +
</source>
  
−
status)
 
−
# Count how many processes we need
 
−
PROCCOUNT=0
 
−
for i in $SERVICES; do
 
−
PROCCOUNT=`expr $PROCCOUNT + 1`
 
−
done
 
  
−
# Go through server PID files and count how many are running
+
== CentOS/RHEL 6 and older script ==
−
log_begin_msg "$NAME: Running processed:"
+
−
_pidCount=0
+
−
for server in $SERVICES; do
+
  
−
isrunning $server
 
−
case "$ISRUNNING" in
 
−
1)
 
−
# This server is running
 
−
_pidCount=`expr $_pidCount + 1`
 
−
log_progress_msg "$server"
 
−
&amp;nbsp;;;
 
−
0)
 
−
&amp;nbsp;;;
 
−
esac
 
−
done
 
  
−
log_begin_msg " ($_pidCount of $PROCCOUNT processes are running)"; log_end_msg 0
+
<source lang="bash">
  
−
# Check if running proc count matches requires proc count
+
#!/bin/bash
−
log_begin_msg "$NAME is"
+
#
−
if [ $_pidCount -eq $PROCCOUNT ]; then
+
# OpenSim      OpenSim
−
log_progress_msg "running"; log_end_msg 0
+
# author neowdj
−
exit 0
+
# date            20131222
−
else
+
# version        0.1
−
log_progress_msg "NOT running"; log_end_msg 0
+
−
exit 1
+
# chkconfig: 2345 80 30
−
fi
+
# description: OpenSim Server
−
&amp;nbsp;;;
+
# processname: mono
  
−
restart)
+
. /etc/rc.d/init.d/functions
−
$0 stop
+
 
−
$0 start
+
#BIN dir
−
&amp;nbsp;;;
+
dir="/opt/opensim/bin/"
−
*)
+
# Robust or OpenSim
−
echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}" &amp;gt;&amp;amp;2
+
prog="Robust"
−
exit 1
+
#Launch program with the user opensim
−
&amp;nbsp;;;
+
user="opensim"
 +
 
 +
 
 +
CMD="su $user -c 'screen -dmS $prog mono --server $prog.exe'"
 +
RUNNING=`ps -e | grep mono | wc -l`
 +
SUBSYS="/var/lock/subsys/mono"
 +
PIDFILE="/var/run/OpenSim/$prog.pid"
 +
OPENSIM_TIMEOUT="100"
 +
 
 +
start() {
 +
    if [ "$RUNNING" -eq 1 ]; then
 +
    echo -n $"Starting $prog: "
 +
                RETVAL=$?
 +
                echo
 +
 
 +
else
 +
cd $dir
 +
echo -n $"Starting $prog: "
 +
                daemon $CMD
 +
        RETVAL=$?
 +
        echo
 +
        [ $RETVAL -eq 0 ] && touch $SUBSYS || \
 +
          RETVAL=1
 +
return $RETVAL
 +
fi
 +
 
 +
}
 +
 
 +
stop() {
 +
echo -n $"Stopping $prog: "
 +
                        su $user -c "screen -S $prog -p 0 -X stuff quit$(printf \\r)" 2>&1
 +
        RETVAL=$?
 +
if [ $RETVAL -eq 0 ] ; then
 +
rm -f $SUBSYS
 +
timeout=0
 +
                while : ; do
 +
                        [ -f $PIDFILE ] || break
 +
                        if [ $timeout -ge $OPENSIM_TIMEOUT ]; then
 +
                                echo
 +
                                return 1
 +
                        fi
 +
                        sleep 2 && echo -n "."
 +
                        timeout=$((timeout+2))
 +
                done
 +
                echo_success
 +
                echo
 +
        else
 +
                echo_failure
 +
                echo
 +
        fi
 +
        return $RETVAL
 +
 
 +
}
 +
 
 +
statusos() {
 +
        status -p $PIDFILE $prog
 +
        exit 1
 +
}
 +
 
 +
case "$1" in
 +
  start)
 +
        start
 +
        ;;
 +
  stop)
 +
        stop
 +
        ;;
 +
  restart)
 +
stop
 +
start
 +
;;
 +
  status)
 +
        statusos
 +
        ;;
 +
  *)
 +
        echo "Usage: $0 {start|stop|restart|status}"
 
esac
 
esac
 +
exit $?
 +
</source>
  
−
exit 0
+
 
−
</pre>
+
= Other OpenSimulator Linux scripts =
 +
 
 +
* [[Tmux]] - Using tmux, a terminal multiplexer, to run OpenSimulator on server while retaining access to the terminal
 +
* [[Ogltree_Install_Instructions|Ogltree Install Instructions]] - Adam Frisby's Linux ogltree scripts
 +
* [https://github.com/ManfredAabye/opensimMULTITOOLS opensimMULTITOOL] - Manfred Aabey's 25,000-line bash script, with approximately 644 functions, for Ubuntu 18.04-24.04 64-bit, DOTNET, mySQL, MariaDB, with screen functions similar to Tmux.

Latest revision as of 19:05, 24 November 2026

Contents

[edit] Systemd Service Units for Linux

[edit] Systemd Service Unit (Unit File) for Rocky/RHEL 10

Modern GNU/Linux enterprise distros have complex security mechanisms like selinux and require more involved configurations to securely run OpenSimulator. This is an example Unit File that will run a stand-alone OpenSimulator as a service via TMUX so that it can run in the background but still allow a system administrator to access the OpenSimulator console when needed. It will also issue a shutdown command to OpenSimulator when the service is stopped to ensure a clean shutdown.

Unit File - opensim.service

[Unit]
Description=OpenSimulator Virtual World Server
After=network.target
 
[Service]
Type=simple
User=opensim
Group=opensim
WorkingDirectory=/opt/opensim/bin
 
Environment=DOTNET_ROOT=/usr/lib64/dotnet
Environment=HOME=/opt/opensim
Environment=TMUX_TMPDIR=/tmp
Environment=TERM=xterm-256color
 
# Start using script to get tty so we can maintain secure tmp and systemd active status
ExecStart=/usr/bin/script -q -c "/usr/bin/tmux new-session -s opensim  /usr/bin/dotnet OpenSim.dll -console=local" /dev/null
 
# Stop by sending opensim the shutdown command via tmux and waiting for process to end
ExecStop=/bin/sh -c 'TMUX_TMPDIR=/tmp /usr/bin/tmux send-keys -t opensim "shutdown" Enter; while pgrep -u opensim -f "^/usr/bin/dotnet .*OpenSim\\.dll" >/dev/null 2>&1; do sleep 1; done'
 
# Opensim can take a while to safely shutdown, you may need to increase this value
TimeoutStopSec=120
 
# Uncomment if you want systemd to automatically restart OpenSimulator
#Restart=on-failure
#RestartSec=15
 
LimitNOFILE=65536
NoNewPrivileges=true
ProtectSystem=full
ProtectHome=true
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

Bash script to simplify console attachment - opensim-attach

#!/bin/bash
#
# This bash script will find the PID of the running simulator
#  service and attach to the tmux session handling the console.
#
# To detach from the console without killing opensimulator, the
# tmux command is Ctrl-b, d
#
sudo nsenter -t "$(systemctl show -p MainPID --value opensim.service)" -m --   runuser -u opensim -- env TMUX_TMPDIR=/tmp tmux attach -t opensim

Setup

  • Ensure tmux is installed
 sudo dnf install tmux
  • Install opensim in /opt/opensim/bin
  • Create a user account for the OpenSimulator service that does not allow logins
 sudo useradd --system --home-dir /opt/opensim --create-home --shell /usr/sbin/nologin opensim
  • Save the above unit file file as /etc/systemd/system/opensim.service
  • Save the above bash script as opensim-attach and make it executable

Usage

 systemctl enable opensim    # Enable the OpenSimulator service so it can be started and stopped
 systemctl disable opensim   # Disable the OpenSimulator service
 systemctl status opensim    # Check if the OpenSimulator service is running
 systemctl start opensim     # Start the OpenSimulator service
 systemctl stop opensim      # Stop the OpenSimulator service
 systemctl restart opensim   # Restart the OpenSimulator service (stop + start)
 journalctl -f -u opensim    # Tail the OpenSimulator service log output (script/tmux output and systemd error/warnings)
 opensim-attach              # Attach to the OpenSimulator console (to detach the command is Ctrl-b, d )


[edit] Systemd Service Unit / Unit File for CentOS/RHEL 7 and 8

# Systemd Service Unit for OpenSimulator 
# Author: steevithak
#
[Unit]
Description=OpenSimulator service
After=network.target
 
[Service]
User=opensim
Group=opensim
Type=simple
LimitSTACK=1048576
TimeoutStopSec=60
WorkingDirectory=/opt/opensim/bin
ExecStart=/usr/bin/mono --server /opt/opensim/bin/OpenSim.exe
 
[Install]
WantedBy=multi-user.target

Notes

  • Should work on CentOS 7.x, CentOS 8.x, RHEL 7.x, RHEL 8.x. Might work on other Redhat-based distros.
  • The "--server" option sets default garbage collection in mono which might be slightly faster for production usage. Using "--desktop" instead should also work and will set the garbage collection system to avoid expanding the heap as much as possible at the expense of slowing down garbage collection a bit.
  • There's a default max timeout of 60 seconds for shutdown. Adjust as needed if that's not long enough on your server.
  • This example doesn't use screen so the OpenSim console will not be available directly.

Setup

  • Create an opensim user (be sure to set shell to /sbin/nologin)
  • Install opensim in /opt/opensim
  • Save the above file as /etc/systemd/system/opensim.service

Usage

  • systemctl enable opensim
  • systemctl disable opensim
  • systemctl status opensim
  • systemctl start opensim
  • systemctl stop opensim
  • systemctl restart opensim
  • journalctl -u opensim

[edit] SysVinit Scripts for very old Linux Distros

[edit] Instructions

Tested on Debian 5. CHUIDS user to opensim user. Supports: start, stop, restart, status

To implement: Type: cat > /etc/init.d/opensim Paste in script from this page Press: CTRL+D Type: chmod +x /etc/init.d/opensim Script should now be located on server and be executable.

Modify header in script to adapt it to your installation folder and user.

[edit] Updated script

Unlike Tedd's original script below, this relies on OpenSimulator's own PID functionality to work, so you must uncomment the PIDFile entries in the [Startup] section of OpenSim.ini/Robust.ini and set them to /tmp/OpenSim.exe.pid and /tmp/Robust.exe.pid as appropriate.

This allows us to simplify the script and get better error reporting. Cleanly shutting down OpenSimulator has also been implemented.

The delay between attempting clean shutdown and force shutting down has been doubled to 20 seconds. However, in some cases this still might not be enough. In the future, it would be better to poll for status.

This script is maintained at https://github.com/justincc/opensimulator-tools/blob/master/infrastructure/control/init.d/src/opensim Pull requests welcome!

#! /bin/sh
### BEGIN INIT INFO
# Provides:          OpenSimulator
# Required-Start:    $local_fs $network 
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Tedds OpenSimulator init.d-script, with further changes by Justin Clark-Casey (http://justincc.org)
### END INIT INFO
 
# Version 0.2.1
# Put script in /etc/init.d/
# Then execute /etc/init.d/opensim <start>|<stop>|<restart>|<status>
#
# You must configure Robust and OpenSim to create Robust.exe.pid and OpenSim.exe.pid PID files
# by configuring PIDFile in the [Startup] section of OpenSim.ini and Robust.ini
 
set -e
 
# Location of OpenSimulator binaries
DIR=/opt/opensim/bin
 
# The directory where OpenSimulator and Robust are placing their pid files.  These must be of the form <service-name>.pid
# e.g. OpenSim.exe.pid
PIDDIR=/tmp/
 
# The user name which will execute the services
USER=opensim
 
SERVICES="Robust.exe OpenSim.exe"
#SERVICES="Robust.exe"
#SERVICES="OpenSim.exe"
 
#
# Kill values (in seconds)
#
# How long between each service being started
DELAY_STARTUP=10
# How long between each service is sent shutdown command
DELAY_KILL=20
# After shutdown has been sent to all we do another loop with "kill", then "kill -9". How long between "kill" and "kill -9".
DELAY_FORCEKILL=10
 
#
# Info on service handled by this script
 
# Name of service
NAME=opensim
# Description of service
DESC="OpenSimulator Server"
 
# Binaries
SCREEN=/usr/bin/screen
MONO=/usr/bin/mono
 
###########################
##### START OF SCRIPT #####
###########################
 
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
 
# Load LSB log functions
_lsbFile=""
if [ -e /etc/debian_version ]; then
    _lsbFile="/lib/lsb/init-functions"
    if [ -f $_lsbFile ]; then
        . $_lsbFile
    else
        echo "This script requires LSB init-functions file which does not exist: $_lsbFile"
        exit 1
    fi
else
# [ -e /etc/init.d/functions ] ; then
    _lsbFile="/etc/init.d/functions"
    if [ -e $_lsbFile ]; then
        . $_lsbFile
    else
        echo "This script requires LSB init-functions file which does not exist: $_lsbFile"
        exit 1
    fi
fi
 
# Lets use fancy output
log_use_fancy_output
 
# Check that target directory exists
if test ! -d "$DIR"; then
    log_failure_msg "$NAME" "Target directory \"$DIR\" does not exist. Can not continue."
    exit 1
fi
 
# Create a reverse order for shutdown
SERVICES_REVERSE=""
reverse() { SERVICES_REVERSE="$9 $8 $7 $6 $5 $4 $3 $2 $1"; }
reverse $SERVICES
 
# Check if a service is running
isrunning() { 
    ISRUNNING="0"
    # Do we have PID-file?
    if [ -f "$PIDDIR/$1.pid" ]; then
        # Check if proc is running
        pid=`cat "$PIDDIR/$1.pid" 2> /dev/null`
        if [ "$pid" != "" ]; then
            if [ -d /proc/$pid ]; then
                # Process is running
                ISRUNNING="1"
            fi
        fi
    fi
    #ISRUNNING="0"
}
 
#
# Process commands
#
case "$1" in
start)
    # Start all services one by one
    for server in $SERVICES; do
        log_daemon_msg "Starting $NAME" "$server"
 
        isrunning $server
        case "$ISRUNNING" in
            1) log_daemon_msg "Process already started. Please stop first"; log_end_msg 1 ;;
            0) 
                # Process is not running
                # Start process and sleep...
                exefile="/tmp/exe.OpenSim.$server.sh"
 
                # Need to make external file, had big problems with screen params
                echo \#\!/bin/bash > $exefile
                echo cd $DIR >> $exefile
                echo $MONO --debug $server >> $exefile
                chmod +x $exefile
 
                cmd_screen="-S $server -d -m $exefile"
                start-stop-daemon --start --pidfile $PIDDIR/$server.pid -u $USER --chdir $DIR --chuid $USER -x /usr/bin/screen -- $cmd_screen
 
                # Delay between services that are started
                sleep $DELAY_STARTUP
 
                rm $exefile 2> /dev/null
 
                isrunning $server
                case "$ISRUNNING" in
                    1) 
                        # Process started ok
                        #log_daemon_msg "Success"; 
                        log_end_msg 0 
                        ;;
                    0) 
                        #log_daemon_msg "Failure"; 
                        log_end_msg 1 
                        ;;
                esac
                ;;
        esac
    done
    ;;
 
stop)
    _killCount=0
 
    for server in $SERVICES_REVERSE; do
        log_daemon_msg "Stopping $NAME" "$server"
 
        isrunning $server
        case "$ISRUNNING" in
        1) 
            _killCount=`expr $_killCount + 1`
            log_daemon_msg "Sending shutdown command:"; 
 
            cmd_screen="$SCREEN -S $server -p 0 -X stuff quit$(printf \\r)"
 
            # We can't use start-stop-daemon here currently because it will only send a signal to OpenSim
            # --stop doesn't execute a command
            # start-stop-daemon --stop --pidfile $PIDDIR/$server.pid -u $USER --chuid $USER -- $cmd_screen
 
            su $USER -c "$cmd_screen"
 
            # Wait for it to shut down...
            sleep $DELAY_KILL
 
            isrunning $server
            case "$ISRUNNING" in
                1) log_daemon_msg "$server is still running."; log_end_msg 0 ;;
                0) log_daemon_msg "$server has been shutdown"; log_end_msg 0 ;;
            esac
 
            ;;
        0) 
            log_daemon_msg "$server is not running"; log_end_msg 0
            ;;
        esac
 
    done
 
    # Check if any procs are still running
    for server in $SERVICES; do
        isrunning $server
        case "$ISRUNNING" in
        1) 
            log_warning_msg "Stopping $NAME" "$server is still running: Forcing kill"
            echo `kill $pid 2> /dev/null`;
            sleep $DELAY_FORCEKILL
            echo `kill -9 $pid 2> /dev/null`;
            sleep 1
 
            # Now check again if it is still running...
            isrunning $server
            case "$ISRUNNING" in
                0) log_daemon_msg "Success"; log_end_msg 0 ;;
                1) log_daemon_msg "Process is still running... Even after \"kill -9 $pid\". WOW..."; log_end_msg 0 ;;
            esac
            ;;
        0) 
            #log_daemon_msg ""; 
            ;;
        esac
    done
 
    log_daemon_msg "$NAME: All done (stopped $_killCount processes)"; log_end_msg 0
 
    ;;
 
status)
    # Count how many processes we need
    PROCCOUNT=0
    for i in $SERVICES; do
        PROCCOUNT=`expr $PROCCOUNT + 1`
    done
 
    # Go through server PID files and count how many are running
    log_daemon_msg "$NAME: Running processes:"
    _pidCount=0
    for server in $SERVICES; do
 
        isrunning $server
        case "$ISRUNNING" in
            1) 
                # This server is running
                _pidCount=`expr $_pidCount + 1`
                log_daemon_msg "$server"
                ;;
            0) 
                ;;
        esac
    done
 
    log_daemon_msg " ($_pidCount of $PROCCOUNT processes are running)"
 
    # Check if running proc count matches requires proc count
    if [ $_pidCount -eq $PROCCOUNT ]; then
        log_daemon_msg "$NAME is running"
        exit 0
    else
        log_daemon_msg "$NAME is NOT running"
        exit 1
    fi
    ;;
 
restart)
    $0 stop
    $0 start
    ;;
*)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}" >&2
    exit 1
    ;;
esac
 
exit 0

[edit] Tedd's Original Script plus clean shutdown tweak

#! /bin/sh
 ### BEGIN INIT INFO
 # Provides:          OpenSimulator
 # Required-Start:    $local_fs $network 
 # Required-Stop:     $local_fs
 # Default-Start:     2 3 4 5
 # Default-Stop:      0 1 6
 # Short-Description: Tedds OpenSimulator init.d-script
 ### END INIT INFO
 
 # Put script in /etc/init.d/
 # then execute: update-rc.d opensim defaults
 
 set -e
 
 #
 # Directories
 #
 # Location of OpenSimulator installation
 DIR=/home/opensim/OpenSim/bin/
 # Different PID dir?
 PIDDIR=$DIR
 
 USER=opensim
 SERVICES="Robust.exe OpenSim.exe"
 
 #
 # Kill values (in seconds)
 #
 # How long between each service being started
 DELAY_STARTUP=10
 # How long between each service is sent shutdown command
 DELAY_KILL=10
 # After shutdown has been sent to all we do another loop with "kill", then "kill -9". How long between "kill" and "kill -9".
 DELAY_FORCEKILL=10
 
 #
 # Info on service handled by this script
 
 # Name of service
 NAME=opensim
 # Description of service
 DESC="OpenSimulator Server"
 
 # Binaries
 SCREEN=/usr/bin/screen
 MONO=/usr/bin/mono
 
 ###########################
 ##### START OF SCRIPT #####
 ###########################
 
 export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
 
 
 
 # Load LSB log functions
 _lsbFile=""
 if [ -e /etc/debian_version ]; then
     _lsbFile="/lib/lsb/init-functions"
     if [ -f $_lsbFile ]; then
         . $_lsbFile
     else
         echo "This script requires LSB init-functions file which does not exist: $_lsbFile"
         exit 1
     fi
 else
 # [ -e /etc/init.d/functions ] ; then
     _lsbFile="/etc/init.d/functions"
     if [ -e $_lsbFile ]; then
         . $_lsbFile
     else
         echo "This script requires LSB init-functions file which does not exist: $_lsbFile"
         exit 1
     fi
 fi
 
 # Lets use fancy output
 log_use_fancy_output
 
 # Check that target directory exists
 if test ! -d "$DIR"; then
     log_failure_msg "$NAME" "Target directory \"$DIR\" does not exist. Can not continue."
     exit 1
 fi
 
 # Create a reverse order for shutdown
 SERVICES_REVERSE=""
 reverse() { SERVICES_REVERSE="$9 $8 $7 $6 $5 $4 $3 $2 $1"; }
 reverse $SERVICES
 
 # Check if a service is running
 isrunning() { 
     ISRUNNING="0"
     # Do we have PID-file?
     if [ -f "$PIDDIR/$1.pid" ]; then
         # Check if proc is running
         pid=`cat "$PIDDIR/$1.pid" 2> /dev/null`
         if [ "$pid" != "" ]; then
             if [ -d /proc/$pid ]; then
                 # Process is running
                 ISRUNNING="1"
             fi
         fi
     fi
     #ISRUNNING="0"
 }
 
 #
 # Process commands
 #
 case "$1" in
 start)
     # Start all services one by one
     for server in $SERVICES; do
         log_daemon_msg "Starting $NAME" "$server"
 
         isrunning $server
         case "$ISRUNNING" in
             1) log_progress_msg "Process already started. Please stop first"; log_end_msg 1 ;;
             0) 
                 # Process is not running
                 # Start process and sleep...
                 exefile="/tmp/exe.OpenSim.$server.sh"
 
                 # Need to make external file, had big problems with screen params
                 echo \#\!/bin/bash > $exefile
                 echo cd $DIR >> $exefile
                 echo $MONO $server >> $exefile
                 chmod +x $exefile
 
                 cmd_screen="screen -S $server -d -m $exefile"
                 start-stop-daemon --start -b -x /bin/su -m --pidfile=/tmp/su.$server.pid --chdir "$DIR" -- - $USER "-c $cmd_screen"
 
                 # Delay between services that are started
                 sleep $DELAY_STARTUP
 
                 rm $exefile 2> /dev/null
 
                 # Make PID-file
                 ps afxu | grep "mono $server" | grep -iEv "grep|screen" | awk {'print $2'} > "$PIDDIR/$server.pid"
 
                 isrunning $server
                 case "$ISRUNNING" in
                     1) 
                         # Process started ok
                         #log_progress_msg "Success"; 
                         log_end_msg 0 
                         ;;
                     0) 
                         # Process did not start... remove pid-file
                         rm "$PIDDIR/$server.pid" 2> /dev/null
                         #log_progress_msg "Failure"; 
                         log_end_msg 1 
                         ;;
                 esac
                 ;;
         esac
 
     done
     ;;
 
 
 
 stop)
     _killCount=0
 
     for server in $SERVICES_REVERSE; do
         log_daemon_msg "Stopping $NAME" "$server"
 
         isrunning $server
         case "$ISRUNNING" in
         1) 
             _killCount=`expr $_killCount + 1`
             log_progress_msg "Sending shutdown command:"; 
 
             cmd_screen="screen -S $server -p 0 -X stuff quit$(printf \\r)"
             su $USER -c "$cmd_screen"
 
             # Wait for it to shut down...
             sleep $DELAY_KILL
 
             isrunning $server
             case "$ISRUNNING" in
                 1) log_progress_msg "is still running."; log_end_msg 0 ;;
                 0) log_progress_msg "has been shutdown"; log_end_msg 0 ;;
             esac
 
             ;;
         0) 
             log_progress_msg "is not running"; log_end_msg 0
             ;;
         esac
 
     done
 
     # Check if any procs are still running
     for server in $SERVICES; do
         isrunning $server
         case "$ISRUNNING" in
         1) 
             log_warning_msg "Stopping $NAME" "$server is still running: Forcing kill"
             echo `kill $pid 2> /dev/null`;
             sleep $DELAY_FORCEKILL
             echo `kill -9 $pid 2> /dev/null`;
             sleep 1
 
             # Now check again if it is still running...
             isrunning $server
             case "$ISRUNNING" in
                 0) log_progress_msg "Success"; log_end_msg 0 ;;
                 1) log_progress_msg "Process is still running... Even after \"kill -9 $pid\". WOW..."; log_end_msg 0 ;;
             esac
             ;;
         0) 
             #log_progress_msg ""; 
             ;;
         esac
     done
 
     log_begin_msg "$NAME: All done (killed $_killCount procs)"; log_end_msg 0
 
     ;;
 
 
 
 
 
 status)
     # Count how many processes we need
     PROCCOUNT=0
     for i in $SERVICES; do
         PROCCOUNT=`expr $PROCCOUNT + 1`
     done
 
     # Go through server PID files and count how many are running
     log_begin_msg "$NAME: Running processed:"
     _pidCount=0
     for server in $SERVICES; do
 
         isrunning $server
         case "$ISRUNNING" in
             1) 
                 # This server is running
                 _pidCount=`expr $_pidCount + 1`
                 log_progress_msg "$server"
                 ;;
             0) 
                 ;;
         esac
     done
 
     log_begin_msg " ($_pidCount of $PROCCOUNT processes are running)"; log_end_msg 0
 
     # Check if running proc count matches requires proc count
     log_begin_msg "$NAME is"
     if [ $_pidCount -eq $PROCCOUNT ]; then
         log_progress_msg "running"; log_end_msg 0
         exit 0
     else
         log_progress_msg "NOT running"; log_end_msg 0
         exit 1
     fi
     ;;
 
 restart)
     $0 stop
     $0 start
     ;;
 *)
     echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}" >&2
     exit 1
     ;;
 esac
 
 exit 0


[edit] CentOS/RHEL 6 and older script

#!/bin/bash
#
# OpenSim      OpenSim
# author		 neowdj
# date            20131222
# version         0.1
 
# chkconfig: 2345 80 30
# description: OpenSim Server
# processname: mono
 
. /etc/rc.d/init.d/functions
 
#BIN dir
dir="/opt/opensim/bin/"
# Robust or OpenSim
prog="Robust"
#Launch program with the user opensim 
user="opensim"
 
 
CMD="su $user -c 'screen -dmS $prog mono --server $prog.exe'"
RUNNING=`ps -e | grep mono | wc -l`
SUBSYS="/var/lock/subsys/mono"
PIDFILE="/var/run/OpenSim/$prog.pid"
OPENSIM_TIMEOUT="100"
 
start() {
    	if [ "$RUNNING" -eq 1 ]; then
     		echo -n $"Starting $prog: "
                RETVAL=$?
                echo
 
	else
		cd $dir
		echo -n $"Starting $prog: "
                daemon $CMD
        	RETVAL=$?
        	echo
        	[ $RETVAL -eq 0 ] && touch $SUBSYS || \
           		RETVAL=1
		return $RETVAL
	fi
 
}
 
stop() {
			echo -n $"Stopping $prog: "
                        su $user -c "screen -S $prog -p 0 -X stuff quit$(printf \\r)" 2>&1
        		RETVAL=$?
			if [ $RETVAL -eq 0 ] ; then
			rm -f $SUBSYS
			timeout=0
	                while : ; do
                        [ -f $PIDFILE ] || break
                        if [ $timeout -ge $OPENSIM_TIMEOUT ]; then
                                echo
                                return 1
                        fi
                        sleep 2 && echo -n "."
                        timeout=$((timeout+2))
                	done
                	echo_success
                	echo
        	else
                	echo_failure
                	echo
        	fi
        return $RETVAL
 
}	
 
statusos() {
        status -p $PIDFILE $prog
        exit 1
}
 
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
	stop
	start
	;;
  status)
        statusos
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|status}"
esac
exit $?


[edit] Other OpenSimulator Linux scripts

  • Tmux - Using tmux, a terminal multiplexer, to run OpenSimulator on server while retaining access to the terminal
  • Ogltree Install Instructions - Adam Frisby's Linux ogltree scripts
  • opensimMULTITOOL - Manfred Aabey's 25,000-line bash script, with approximately 644 functions, for Ubuntu 18.04-24.04 64-bit, DOTNET, mySQL, MariaDB, with screen functions similar to Tmux.
Personal tools
General
About This Wiki