Skip to content

Instantly share code, notes, and snippets.

@gofer
Created October 16, 2024 06:41
Show Gist options
  • Save gofer/0b63e4fc6023cb26bc6c02b4d5f8466b to your computer and use it in GitHub Desktop.
Save gofer/0b63e4fc6023cb26bc6c02b4d5f8466b to your computer and use it in GitHub Desktop.
PythonのHTTPサーバーをよしなにやるスクリプト

PythonのHTTPサーバーをよしなにやるスクリプト

設定

環境変数 設定内容 既定値
PID_FILE プロセスIDを格納しておくファイルへのパス /www.pid
PORT ポート番号 80
WWW_DIR ドキュメントルート $(pwd)

開始

bash web.bash start

終了

bash web.bash stop
#!/bin/bash
PID_FILE=/www.pid
PORT=80
WWW_DIR="$(pwd)"
function start() {
if [ -f "$PID_FILE" ]; then
echo 'Server has already run' 1>&2
exit 1
fi
cd $WWW_DIR
python3 -m http.server "$PORT" &
echo $! > "$PID_FILE"
}
function stop() {
if [ ! -f "$PID_FILE" ]; then
echo 'No PID file.' 1>&2
exit 1
fi
pid=$(cat "$PID_FILE")
kill -9 $pid
rm "$PID_FILE"
}
cmd=$1
if [ "$cmd" = 'start' ]; then
start
elif [ "$cmd" = 'stop' ]; then
stop
else
echo 'Invalid command' 1>&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment