Further imporvements to sunshine-run and its logging

This commit is contained in:
Josh.5
2023-09-11 11:54:30 +12:00
parent 608f772ccc
commit 0981af0a17
2 changed files with 84 additions and 44 deletions

View File

@@ -10,7 +10,7 @@
### ###
set -e set -e
exec >> >(tee -a /tmp/sunshine-exec-run.log) 2>&1 exec >> >(tee -a ${USER_HOME:?}/.cache/log/sunshine_run.log) 2>&1
echo echo
echo "-------------------------------" echo "-------------------------------"
echo echo
@@ -33,11 +33,18 @@ trap '_term QUIT' SIGQUIT
trap '_term HUP' SIGHUP trap '_term HUP' SIGHUP
trap '_term TERM' SIGTERM trap '_term TERM' SIGTERM
# Record sunshine-run PID
echo " - Recording sunshine-run PID '$$' in /tmp/sunshine-exec-run.pid"
echo "$$" > /tmp/sunshine-exec-run.pid
# Wipe session config # Wipe session config
echo "" > /tmp/sunshine-exec-run.conf echo "" > /tmp/sunshine-exec-run.conf
# Update display resolution and refresh reate if client provided the required details # Update display resolution and refresh reate if client provided the required details
if ([ "X${SUNSHINE_CLIENT_WIDTH:-}" != "X" ] && [ "X${SUNSHINE_CLIENT_HEIGHT:-}" != "X" ] && [ "X${SUNSHINE_CLIENT_FPS:-}" != "X" ]); then if ([ "X${SUNSHINE_CLIENT_WIDTH:-}" != "X" ] && [ "X${SUNSHINE_CLIENT_HEIGHT:-}" != "X" ] && [ "X${SUNSHINE_CLIENT_FPS:-}" != "X" ]); then
# Don't fail script if something goes wrong here. Set +e in subprocess
(
set +e
echo " - Setting display resolution to ${SUNSHINE_CLIENT_WIDTH:?}x${SUNSHINE_CLIENT_HEIGHT:?} ${SUNSHINE_CLIENT_FPS:?}Hz" echo " - Setting display resolution to ${SUNSHINE_CLIENT_WIDTH:?}x${SUNSHINE_CLIENT_HEIGHT:?} ${SUNSHINE_CLIENT_FPS:?}Hz"
# Read current display mode and extract resolution and refresh rate from the mode string # Read current display mode and extract resolution and refresh rate from the mode string
@@ -71,19 +78,23 @@ if ([ "X${SUNSHINE_CLIENT_WIDTH:-}" != "X" ] && [ "X${SUNSHINE_CLIENT_HEIGHT:-}"
echo "primary_output='${__primary_output:?}'" >> /tmp/sunshine-exec-run.conf echo "primary_output='${__primary_output:?}'" >> /tmp/sunshine-exec-run.conf
echo "display_mode='${__current_resolution:?}'" >> /tmp/sunshine-exec-run.conf echo "display_mode='${__current_resolution:?}'" >> /tmp/sunshine-exec-run.conf
echo "display_rate='${__current_refresh_rate:?}'" >> /tmp/sunshine-exec-run.conf echo "display_rate='${__current_refresh_rate:?}'" >> /tmp/sunshine-exec-run.conf
)
fi fi
# Run child process # Run child process
/usr/bin/dumb-init "${@}" & /usr/bin/dumb-init "${@}" &
proc_pid=$! proc_pid=$!
echo " - Recording sunshine-run PID '${proc_pid}' in /tmp/sunshine-exec-run.pid"
echo "${proc_pid}" > /tmp/sunshine-exec-run.pid
# Wait for child process to exit: # Wait for child process to exit:
echo " - Waiting for PID '${proc_pid}' to exit" echo " - Waiting for PID '${proc_pid}' to exit"
wait "$proc_pid" wait "$proc_pid"
# Clean up PID file # Clean up PID file
rm -f /tmp/sunshine-exec-run.pid if [ -f /tmp/sunshine-exec-run.pid ]; then
echo " - Removing sunshine-run pidfile."
rm -f /tmp/sunshine-exec-run.pid
else
echo " - WARNING: No sunshine-run pidfile found for removal."
fi
echo "DONE" echo "DONE"

View File

@@ -10,7 +10,7 @@
### ###
set -e set -e
exec >> >(tee -a /tmp/sunshine-exec-stop.log) 2>&1 exec >> >(tee -a ${USER_HOME:?}/.cache/log/sunshine_stop.log) 2>&1
echo echo
echo "-------------------------------" echo "-------------------------------"
echo echo
@@ -23,52 +23,81 @@ echo
# Read the run config # Read the run config
if [ -f /tmp/sunshine-exec-run.conf ]; then if [ -f /tmp/sunshine-exec-run.conf ]; then
echo " - Reading sunshine-run config."
. /tmp/sunshine-exec-run.conf . /tmp/sunshine-exec-run.conf
else
echo " - WARNING: No sunshine-run config found for import."
fi fi
# Read the process PID # Read the process PID
process_pid="$(cat /tmp/sunshine-exec-run.pid)" process_pid="$(cat /tmp/sunshine-exec-run.pid)"
echo " - Found initial sunshine-run PID '${process_pid}'" echo " - Found initial sunshine-run PID '${process_pid}'."
# Send a INT signal to the PID # Send a INT signal to the PID
echo " - Sending SIGINT to PID '${process_pid}'" echo " - Sending SIGINT to PID '${process_pid}'."
kill -INT "${process_pid}" kill -INT "${process_pid}" || true
# Run a loop to check for any other "sunshine-run" processes and send them also a INT signal # Run a loop to check for any other "sunshine-run" processes and send them also a INT signal
echo " - Checking for other sunshine-run processes '${process_pid}'" echo " - Checking for other sunshine-run processes..."
for process_pid in $(ps aux | grep -v grep | grep sunshine-run | awk '{print $2}'); do for process_pid in $(ps aux | grep -v grep | grep sunshine-run | awk '{print $2}'); do
echo " - Sending SIGINT to PID '${process_pid}'" # Ensure this process is still running
kill -INT "${process_pid}" 2>/dev/null if ! kill -0 "$process_pid" 2>/dev/null; then
continue
fi
echo " - Stopping PID '${process_pid}' - $(ps -p ${process_pid} -o command | awk 'NR>1')"
# Send this process a SIGINT
echo " - Sending SIGINT to PID '${process_pid}'."
kill -INT "${process_pid}" 2>/dev/null || true
sleep 0.5 sleep 0.5
# Send this process a SIGTERM every 2 seconds util stopped or until it has sent 3 signals
counter=0 counter=0
while kill -0 "$process_pid" 2>/dev/null; do while kill -0 "$process_pid" 2>/dev/null; do
echo " - Sending SIGTERM to PID '${process_pid}'" echo " - Sending SIGTERM to PID '${process_pid}'."
kill -TERM "$process_pid" 2>/dev/null kill -TERM "$process_pid" 2>/dev/null || true
counter=$((counter + 1)) counter=$((counter + 1))
[ "$counter" -gt 2 ] && break [ "$counter" -gt 2 ] && break
sleep 2 sleep 2
done done
# Send this process a SIGKILL every second until stopped or until it has sent 3 signals
counter=0 counter=0
while kill -0 "$process_pid" 2>/dev/null; do while kill -0 "$process_pid" 2>/dev/null; do
echo " - Sending SIGKILL to PID '${process_pid}'" echo " - Sending SIGKILL to PID '${process_pid}'."
kill -KILL "$process_pid" 2>/dev/null kill -KILL "$process_pid" 2>/dev/null || true
counter=$((counter + 1)) counter=$((counter + 1))
[ "$counter" -gt 2 ] && break [ "$counter" -gt 2 ] && break
sleep 1 sleep 1
done done
if ! kill -0 "$process_pid" 2>/dev/null; then
echo " - PID '${process_pid}' stopped."
else
echo " - ERROR: Failed to kill PID '${process_pid}'."
fi
done done
echo " - All sunshine-run related processes stopped."
# Check if the required information was stored for resetting the display resolution and refresh rate # Check if the required information was stored for resetting the display resolution and refresh rate
if ([ "X${primary_output:-}" != "X" ] && [ "X${display_mode:-}" != "X" ] && [ "X${display_rate:-}" != "X" ]); then if ([ "X${primary_output:-}" != "X" ] && [ "X${display_mode:-}" != "X" ] && [ "X${display_rate:-}" != "X" ]); then
# Reset display resolution and refresh rate # Reset display resolution and refresh rate
echo " - Resetting display resolution to ${display_mode:?} ${display_rate:?}Hz" echo " - Resetting display resolution to ${display_mode:?} ${display_rate:?}Hz"
xrandr --output "${primary_output:?}" --mode "${display_mode:?}" --rate "${display_rate:?}" xrandr --output "${primary_output:?}" --mode "${display_mode:?}" --rate "${display_rate:?}"
else
echo " - WARNING: original display config not found. Resolution will not be reset."
fi fi
# Clean up config file # Clean up config file
rm -f /tmp/sunshine-exec-run.conf if [ -f /tmp/sunshine-exec-run.conf ]; then
echo " - Removing sunshine-run config."
rm -f /tmp/sunshine-exec-run.conf
else
echo " - WARNING: No sunshine-run config found for removal."
fi
# Clean up PID file # Clean up PID file
rm -f /tmp/sunshine-exec-run.pid if [ -f /tmp/sunshine-exec-run.pid ]; then
echo " - Removing sunshine-run pidfile."
rm -f /tmp/sunshine-exec-run.pid
else
echo " - WARNING: No sunshine-run pidfile found for removal."
fi
echo "DONE" echo "DONE"