Skip to content

Instantly share code, notes, and snippets.

@danmackinlay
Last active October 1, 2024 23:34
Show Gist options
  • Save danmackinlay/03a9c50b6719391aaee9291c9615f429 to your computer and use it in GitHub Desktop.
Save danmackinlay/03a9c50b6719391aaee9291c9615f429 to your computer and use it in GitHub Desktop.
try to kill and restart quarto

restart quarto preview server

As per my blog I think quarto is best if you turn it off an on again.

My fix to the many weird bugs and misfeatures in quarto preview is to continually restart preview server. I also try to turn off as much “smart” stuff as possible, disabling the file watcher and the browser navigation. This seems to help both in memory usage and making sure that it serves current content. Currently I do this manually, by running it in a shell and doing a ^C to kill it.

I attempted to define a helper function which kills the quarto process and restarts it automatically, but the wily quarto process seems to evade my attempts to kill it by spawning detached subprocesses or something, so it doesn’t work. Fixes welcome.

quarto_preview_restart --restart-time 300 --port 9888 --no-navigate --no-browser --no-watch-input
function quarto_preview_restart --description "Run quarto preview in a controlled subshell and manage restarts, with checks for premature termination"
# Default wait time to 300 seconds
set wait_time 300
# Process all arguments to find --restart-time and remove it along with its value
set -l arg_count (count $argv)
for i in (seq $arg_count)
if test "$argv[$i]" = "--restart-time"
# Set wait time to the next argument
set wait_time $argv[(math $i+1)]
# Remove the --restart-time argument and its value
set -e argv[$i]
set -e argv[$i]
break
end
end
while true
# Run quarto preview in a new shell as a foreground process
sh -c "quarto preview $argv" &
set pid $last_pid
echo "Started quarto preview within a subshell with PID $pid"
# Initialize a timer
set elapsed_time 0
set check_interval 10
# Monitor the process periodically within the wait time
while test $elapsed_time -lt $wait_time
sleep $check_interval
set elapsed_time (math $elapsed_time + $check_interval)
# Check if the process is still alive
if not kill -0 $pid > /dev/null 2>&1
echo "Process $pid terminated prematurely. Restarting..."
break
end
end
# If the loop completed without breaking, kill the process
if test $elapsed_time -ge $wait_time
kill $pid
if not kill -0 $pid > /dev/null 2>&1
echo "Successfully killed quarto preview with PID $pid"
else
echo "Failed to kill quarto preview with PID $pid"
end
end
# Wait for the quarto process to terminate before restarting
wait $pid
# Optional: wait a moment before restarting
sleep 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment