Skip to content

WSL2 Python server

If you’re on Windows and have WSL2 installed, you can use the following bash script to start a Python HTTP server that serves your Witchcraft scripts folder. You can then use Windows Task Scheduler to run this script automatically at startup.

Let’s start by creating the script:

witchcraft-server.sh
#!/bin/bash
# ~/start-server.sh
# Usage: ./start-server.sh start | stop | status
PORT=5743
SERVER_CMD="python3 -m http.server $PORT"
SERVER_DIR="$HOME/witchcraft-scripts"
LOG_FILE="/tmp/site-http.log"
PID_FILE="/tmp/site-http.pid"
start_server() {
if [ -f "$PID_FILE" ] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then
echo "Server already running (PID $(cat $PID_FILE))"
exit 0
fi
cd "$SERVER_DIR" || exit 1
nohup $SERVER_CMD >"$LOG_FILE" 2>&1 &
echo $! >"$PID_FILE"
echo "Server started (PID $(cat $PID_FILE))"
}
stop_server() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if kill -0 "$PID" 2>/dev/null; then
kill "$PID"
rm -f "$PID_FILE"
echo "Server (PID $PID) stopped"
else
echo "Stale PID file found, removing"
rm -f "$PID_FILE"
fi
else
echo "No PID file found. Server may not be running."
fi
}
status_server() {
if [ -f "$PID_FILE" ] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then
echo "Server is running (PID $(cat $PID_FILE))"
else
echo "Server is not running"
fi
}
case "$1" in
start)
start_server
;;
stop)
stop_server
;;
status)
status_server
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
;;
esac

Make it executable:

Terminal window
chmod +x witchcraft-server.sh

And you can now test it to bring the server up and then down:

Terminal window
./witchcraft-server.sh start
./witchcraft-server.sh stop

With that working, you can now create a task in Windows Task Scheduler to run this script at startup:

  1. Open Task Scheduler (you can find it by searching in the Start menu).
  2. Click on “Create Task…” (not “basic”) in the right-hand pane.
  3. In the “General” tab:
    • Name your task (e.g., “Witchcraft Server”)
    • Configure for “Windows 10/11” or your version of Windows
  4. In the “Triggers” tab:
    • Click “New…”
    • Set “Begin the task” to “At log on”
    • Uncheck “Stop task if it runs longer than…”
  5. In the “Actions” tab:
    • Click “New…”
    • Set “Action” to “Start a program”
    • In the “Program/script” field, enter C:\Windows\System32\wsl.exe
    • In the “Add arguments (optional)” field, enter this: -d Ubuntu -- bash -lc '~/witchcraft-server.sh start' (but double-check the script path to make sure it matches where you saved it in WSL2)

And that’s it. You can now manually run the task to test it, or simply restart your computer and check if the Witchcraft server is running by looking at the extension’s status indicator.