Skip to content

Instantly share code, notes, and snippets.

@physacco
Last active December 14, 2015 09:08
Show Gist options
  • Save physacco/5062289 to your computer and use it in GitHub Desktop.
Save physacco/5062289 to your computer and use it in GitHub Desktop.
GoAgent init script for Redhat/Fedora.
#!/bin/sh
# goagent: GoAgent init script for Redhat/Fedora.
# Written by physacco. 2013/03/01
# To start at boot time:
# 1. copy this file to /etc/init.d/goagent
# 2. chkconfig goagent on
# ---------------------
# chkconfig: 2345 50 50
# ---------------------
# Configuration variables.
PYTHON="$(which python 2>/dev/null)"
DAEMON_DIR="/path/to/your/goagent/local"
DAEMON_USR="root"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
DAEMON="goagent"
DAEMON_LOG="$DAEMON_DIR/goagent.log"
DAEMON_PID="$DAEMON_DIR/goagent.pid"
start()
{
[ -x "$PYTHON" ] || exit 5
echo -n "Starting $DAEMON: "
daemon --user "$DAEMON_USR" "$PYTHON" - goagent <<EOF
from os import *; fork() and exit(); setsid(); fork() and exit()
chdir("$DAEMON_DIR"); umask(0022); f = file("$DAEMON_PID", "w")
f.write(str(getpid())); f.close(); flag = O_WRONLY|O_APPEND|O_CREAT
fd = open("$DAEMON_LOG", flag, 0644); dup2(fd, 1); dup2(fd, 2)
close(0); close(fd); script = 'proxy.py'; modname = '__main__'
execfile(script, {'__file__': script, '__name__': modname})
EOF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n "Stopping $DAEMON: "
killproc -p "$DAEMON_PID"
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
rh_status() {
status -p "$DAEMON_PID" goagent
}
rh_status_q() {
rh_status &>/dev/null
}
case "$1" in
start)
rh_status_q && exit 0
start
;;
stop)
rh_status_q || exit 0
stop
;;
restart|force-reload)
restart
;;
try-restart|condrestart)
rh_status_q || exit 7
restart
;;
reload)
exit 3
;;
status|status_q)
rh_$1
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status}"
exit 2
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment