Snippets
Created by
Ben Buchanan
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | #!/bin/bash
# Open source, MIT license: Copyright (c) 2017 Ben Buchanan. https://opensource.org/licenses/MIT
# This script provides two commands, "server" and "serverstop"
# Enable by saving server.sh locally, and sourcing it in your .bash_profile:
#
# source /path/to/server.sh
#
# Checked with https://www.shellcheck.net
# Tested on OSX, WSL and Cygwin.
# Known issue: detection of running server not 100% reliable.
# Inspired by https://gist.github.com/1525217 but has grown a bit over several years
# SERVER
# Set up a simple python server from current directory.
# Usage:
# server # start on default port
# server 1234 # override default port
function server() {
local host
local port
local openbrowser
local machinehost
local localhost
host=$(hostname)
port="${1:-8888}" # take first arg as port number, or default to 8888
openbrowser=true
machinehost="http://${host}:${port}"
localhost="http://localhost:${port}"
# Check for existing server.
echo "Checking for existing server at ${localhost} (will try for max 15s)"
if curl -sI "${localhost}" --connect-timeout 5 --max-time 15 | grep --silent "200 OK" ;then
echo "Server already running at ${localhost} - choose another port with 'server 1234'"
return
else
echo "Server did not respond within timeout, assuming ${localhost} is available."
fi
# The command to open the browser varies between platforms
case "$(uname -s)" in
CYGWIN*)
opencommand="cygstart"
;;
Darwin*)
opencommand="open"
;;
Linux*)
opencommand="xdg-open"
# check for WSL which initially identifies as Linux
if grep --silent 'Microsoft' /proc/sys/kernel/osrelease; then
opencommand="explorer.exe"
fi
;;
*)
echo "Browser won't open automatically."
openbrowser=false
;;
esac
if [[ $openbrowser ]]; then
echo "Browser will open automatically"
(sleep 1 && ${opencommand} "${localhost}")&
fi
echo "Starting server."
echo "Server should be visible at both ${localhost} and http://${host}:${port}/"
echo "Use ctrl+c to stop server."
python -m SimpleHTTPServer "$port"
}
# SERVERSTOP
# Kills python server. Useful for hung servers or servers running in background processes
# Usage:
# serverstop # kill server without checking port
# serverstop 1234 # kill server on specific port
function serverstop() {
local target
local greptarget
local grepcount
if [[ $1 ]];then
target="SimpleHTTPServer $1"
else
target="SimpleHTTPServer"
fi
greptarget="[p]ython -m $target"
grepcount=$(pgrep -f "$greptarget" | wc -l)
case $grepcount in
0)
echo "No servers running."
;;
1)
echo "Stopping $target"
kill -9 "$(pgrep -f "$greptarget")"
;;
*)
pgrep "$greptarget" | xargs ps -p
echo "Multiple matches, specify port: serverstop 1234"
;;
esac
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.