Skip to content

Instantly share code, notes, and snippets.

@aneek
Forked from josephspurrier/etc-init.d-hello-world
Created December 15, 2019 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aneek/890b456ab30c1773a0b165e3eec4dc0d to your computer and use it in GitHub Desktop.
Save aneek/890b456ab30c1773a0b165e3eec4dc0d to your computer and use it in GitHub Desktop.
/etc/init.d Script for Go Application
#!/bin/bash
#
# chkconfig: 35 95 05
# description: Hello world application.
# Run at startup: sudo chkconfig hello-world on
# Load functions from library
. /etc/init.d/functions
# Name of the application
app="hello"
# Start the service
run() {
echo -n $"Starting $app:"
cd /home/ec2-user/workspace/bin
./$app > /var/log/$app.log 2> /var/log/$app.err < /dev/null &
sleep 1
status $app > /dev/null
# If application is running
if [[ $? -eq 0 ]]; then
# Store PID in lock file
echo $! > /var/lock/subsys/$app
success
echo
else
failure
echo
fi
}
# Start the service
start() {
status $app > /dev/null
# If application is running
if [[ $? -eq 0 ]]; then
status $app
else
run
fi
}
# Restart the service
stop() {
echo -n "Stopping $app: "
killproc $app
rm -f /var/lock/subsys/$app
echo
}
# Reload the service
reload() {
status $app > /dev/null
# If application is running
if [[ $? -eq 0 ]]; then
echo -n $"Reloading $app:"
kill -HUP `pidof $app`
sleep 1
status $app > /dev/null
# If application is running
if [[ $? -eq 0 ]]; then
success
echo
else
failure
echo
fi
else
run
fi
}
# Main logic
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $app
;;
restart)
stop
sleep 1
start
;;
reload)
reload
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|status}"
exit 1
esac
exit 0
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello.")
}
func listen() {
fmt.Println("Webserver started.")
err := http.ListenAndServe(":80", nil)
if err != nil {
fmt.Println(err)
}
}
func reloadable() {
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGHUP)
go func() {
for {
<-s
fmt.Println("Reloaded")
}
}()
}
func main() {
http.HandleFunc("/", handler)
reloadable()
listen()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment