This Bash script sets up a virtual Linux environment to run Quake III Arena and provides remote VNC access, initializing the X11 server and game with customized settings. The script also sets up signal handling, VNC server setup, and stores process IDs for later use, with cleanup functions to terminate all processes when complete.
#!/bin/bash
export DISPLAY=:4000
/usr/bin/Xvfb $DISPLAY -screen 0 1000x600x24 -dpi 70 -ac +extension RANDR & echo $! > /tmp/xvfb.pid
/usr/bin/xinit -display $DISPLAY & echo $! > /tmp/x11.pid
/usr/lib/ioquake3/ioquake3 --args +set fs_game missionpack +exec ./client.cfg & echo $! > /tmp/xterm.pid
sleep 5
/usr/bin/x11vnc -scale 1000x600 -noxdamage -forever -shared -rfbport 5900 -display $DISPLAY -noxrecord & echo $! > /tmp/vnc.pid
int_handler()
{
kill $(cat /tmp/xterm.pid) 2> /dev/null
kill $(cat /tmp/vnc.pid)
kill $(cat /tmp/x11.pid)
kill $(cat /tmp/xvfb.pid)
}
trap 'int_handler' INT
wait $(cat /tmp/xterm.pid)
int_handler
#!/bin/bash
# Set display for Xvfb
DISPLAY=:4000
# Run Xvfb headless session with required settings
Xvfb_PID=$(/usr/bin/Xvfb $DISPLAY -screen 0 1000x600x24 -dpi 70 -ac +extension RANDR & echo $!)
echo $Xvfb_PID > /tmp/xvfb.pid
# Run X11 session
X11_PID=$(/usr/bin/xinit -display $DISPLAY & echo $!)
echo $X11_PID > /tmp/x11.pid
# Run ioquake3 game with specified arguments
IOQ3_PID=$(/usr/lib/ioquake3/ioquake3 --args +set fs_game missionpack +exec./client.cfg & echo $!)
echo $IOQ3_PID > /tmp/xterm.pid
# Wait for 5 seconds to ensure X11 and IOQ3 sessions are up
sleep 5
# Run x11vnc for remote desktop access
VNC_PID=$(/usr/bin/x11vnc -scale 1000x600 -noxdamage -forever -shared -rfbport 5900 -display $DISPLAY -noxrecord & echo $!)
echo $VNC_PID > /tmp/vnc.pid
# Define function to handle INT signal (Ctrl+C)
int_handler() {
# Kill X11vnc session
kill $(cat /tmp/vnc.pid) 2>/dev/null
# Kill IOQ3 game session
kill $(cat /tmp/xterm.pid) 2>/dev/null
# Kill X11 session
kill $(cat /tmp/x11.pid) 2>/dev/null
# Kill Xvfb headless session
kill $(cat /tmp/xvfb.pid) 2>/dev/null
}
# Trap INT signal to call int_handler function
trap 'int_handler' INT
# Wait for IOQ3 game session to finish
wait $(cat /tmp/xterm.pid)
# Call int_handler function to clean up sessions
int_handler
Code Breakdown
This is a Bash script that sets up a virtual Linux environment for running a Quake III Arena game and provides VNC access.
export DISPLAY=:4000
: Sets the display environment variable to :4000
.Xvfb
: Creates a virtual framebuffer with resolution 1000x600x24, DPI 70, and allows RANDR extension.xinit
: Starts the X11 server and displays it on the virtual framebuffer.ioquake3
: Runs the Quake III Arena game with customized settings (fs_game set to missionpack and executes client.cfg).sleep 5
: Waits for 5 seconds to allow the game to initialize.x11vnc
: Sets up VNC server on display :4000
, allowing remote access on port 5900.int_handler
: Functions to handle signals (e.g., interrupt) by killing all running processes.trap
: Sets up the int_handler
function to be executed when a signal is received.wait
and int_handler
: Waits for the game process to exit and then kills all remaining processes.The script stores the process IDs of the game, VNC server, X11 server, and Xvfb in temporary files (/tmp/xterm.pid
, /tmp/vnc.pid
, /tmp/x11.pid
, and /tmp/xvfb.pid
) for later use.