Skip to content

Instantly share code, notes, and snippets.

@sixstringsg
Created July 25, 2018 21:59
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 sixstringsg/733a1a2cd5a14d31d17cfbe57e8142f5 to your computer and use it in GitHub Desktop.
Save sixstringsg/733a1a2cd5a14d31d17cfbe57e8142f5 to your computer and use it in GitHub Desktop.
#!/bin/bash
# chkconfig: 2345 90 90
# description: inetmon
### BEGIN INIT INFO
# Provides: inetmon
# Required-Start: networking
# Required-Stop: networking
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Start the program
### END INIT INFO
### Fill in these bits:
START_CMD="/root/bin/inetmon.sh > /var/log/inetmon.log 2>&1 &"
NAME="inetmon"
PGREP_STRING="/root/bin/inetmon.sh"
PID_FILE="/var/run/inetmon/inetmon.pid"
USER="root"
### No further muckin' about needed!
#CUR_USER=`whoami`
CUR_USER="root"
killproc() {
pkill -u $USER -f $PGREP_STRING
}
start_daemon() {
eval "$*"
}
log_success_msg() {
echo "$*"
logger "$_"
}
log_failure_msg() {
echo "$*"
logger "$_"
}
check_proc() {
pgrep -u $USER -f $PGREP_STRING >/dev/null
}
start_script() {
if [ "${CUR_USER}" != "root" ] ; then
log_failure_msg "$NAME can only be started as 'root'."
exit -1
fi
check_proc
if [ $? -eq 0 ]; then
log_success_msg "$NAME is already running."
exit 0
fi
[ -d /var/run/$NAME ] || (mkdir /var/run/$NAME )
# make go now
start_daemon /bin/su $USER -c "$START_CMD"
# Sleep for a while to see if anything cries
sleep 5
check_proc
if [ $? -eq 0 ]; then
log_success_msg "Started $NAME."
else
log_failure_msg "Error starting $NAME."
exit -1
fi
}
stop_script() {
if [ "${CUR_USER}" != "root" ] ; then
log_failure_msg "You do not have permission to stop $NAME."
exit -1
fi
check_proc
if [ $? -eq 0 ]; then
killproc -p $PID_FILE >/dev/null
# Make sure it's dead before we return
until [ $? -ne 0 ]; do
sleep 1
check_proc
done
check_proc
if [ $? -eq 0 ]; then
log_failure_msg "Error stopping $NAME."
exit -1
else
log_success_msg "Stopped $NAME."
fi
else
log_failure_msg "$NAME is not running or you don't have permission to stop it"
fi
}
check_status() {
check_proc
if [ $? -eq 0 ]; then
log_success_msg "$NAME is running."
else
log_failure_msg "$NAME is stopped."
exit -1
fi
}
case "$1" in
start)
start_script
;;
stop)
stop_script
;;
restart)
stop_script
start_script
;;
status)
check_status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment