Created
March 8, 2023 10:45
-
-
Save for2ando/5aabfae00cee271b3ff458a137b1b8c7 to your computer and use it in GitHub Desktop.
Python3 http.server start/stop script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
pname="$(basename $0)" | |
htdocs=/var/www/htdocs | |
logfile=/var/log/pyhttpd/httpd.log | |
pidfile=/var/run/pyhttpd.pid | |
port=8086 | |
httpd="python3 -m http.server --cgi $port" | |
proc_signature="[0-9]:[0-9][0-9] $httpd" | |
usage="$pname {start|stop|restart|syncpid}" | |
start_proc() { | |
cd "$htdocs" | |
nohup $httpd >"$logfile" 2>&1 & | |
get_pid >"$pidfile" | |
} | |
start_ifdown_proc() { | |
pid=$(get_pid) | |
test -n "$pid" && return | |
start_proc | |
} | |
stop_proc() { | |
test -f "$pidfile" || { echo "$pname: $pidfile not exist.">&2; exit 2; } | |
kill $(cat "$pidfile") && rm -f "$pidfile" | |
} | |
restart_proc() { | |
clean_proc | |
start_proc | |
} | |
clean_proc() { | |
test -f "$pidfile" && stop_proc | |
while true; do | |
pid=$(getpid) | |
test -z "$pid" && break | |
echo "$pid">"$pidfile" | |
stop_proc | |
done | |
} | |
get_pid() { | |
set - $(ps | grep "$proc_signature") | |
echo $1 | |
} | |
sync_pid() { | |
test -f "$pidfile" && return | |
pid=$(getpid) | |
test -n "$pid" && echo "$pid">"$pidfile" | |
} | |
case "$1" in | |
start) | |
start_ifdown_proc;; | |
stop) | |
clean_proc;; | |
restart) | |
restart_proc;; | |
syncpid) | |
sync_pid;; | |
*) | |
echo "$usage"; exit;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment