-
-
Save rahulvramesh/5a4a5bd1562448532d618a8132317ad3 to your computer and use it in GitHub Desktop.
/etc/init.d Script for Go Application
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
#!/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 |
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
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