Skip to content

Instantly share code, notes, and snippets.

@KpuCko
Last active May 12, 2017 10:31
Show Gist options
  • Save KpuCko/298d63177e44ca0af1525b5a4f3ab0a1 to your computer and use it in GitHub Desktop.
Save KpuCko/298d63177e44ca0af1525b5a4f3ab0a1 to your computer and use it in GitHub Desktop.
Simple python HTTP server
#!/bin/sh
#
# pyweb: Starts simple HTTP server
#
# chkconfig: 345 97 03
# description: Starts simple HTTP server
#
# processname: pyweb
# pidfile: /var/run/pyweb.pid
#
### BEGIN INIT INFO
# Provides: pyweb
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts simple HTTP server
# Description: Starts simple HTTP server
### END INIT INFO
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Sanity checks.
[ -x /usr/bin/python ] || exit 6
[ -x /usr/sbin/lsof ] || exit 5
# Source function library.
. /etc/rc.d/init.d/functions
start() {
if [ ! -z $(lsof -i :8080 | awk '{print $2}' |tail -n1) ]; then
echo "Simple HTTP server already started."
exit 1
fi
echo -n $"Starting simple HTTP server..."
python -m SimpleHTTPServer 8080 >/dev/null 2>&1 &
sleep 3
lsof -i :8080 | awk '{print $2}' |tail -n1 > /var/run/pyweb.pid
echo "done"
}
stop() {
if [ ! -f /var/run/pyweb.pid ]; then
echo "Simple HTTP server already stopped."
exit 1
fi
echo -n $"Stopping simple HTTP server... "
kill -9 $(cat /var/run/pyweb.pid)
rm -f /var/run/pyweb.pid
echo "done"
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
if [ ! -z $(lsof -i :8080 | awk '{print $2}' |tail -n1) ]; then
echo "Simple HTTP server is running (pid $(cat /var/run/pyweb.pid))"
else
echo "Simple HTTP server is stopped"
fi
;;
restart|force-reload)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 2
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment