PageRenderTime 118ms CodeModel.GetById 114ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/rc1.d/K20denyhosts

http://github.com/brinkman83/bashrc
Shell | 93 lines | 71 code | 5 blank | 17 comment | 2 complexity | 70fd8a38fc87e804bd5a114046072a5e MD5 | raw file
 1#! /bin/sh 
 2#
 3# Init script for denyhosts
 4#
 5# Author:	Marco Bertorello <marco@bertorello.ns0.it>.
 6#
 7### BEGIN INIT INFO
 8# Provides:          denyhosts
 9# Required-Start:    $syslog $local_fs $time
10# Required-Stop:     $syslog $local_fs
11# Default-Start:     2 3 4 5
12# Default-Stop:	     0 1 6
13# Short-Description: Start denyhosts and watch .
14### END INIT INFO
15
16
17# Using LSB funtions:
18. /lib/lsb/init-functions
19
20set -e
21
22PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
23DESC="DenyHosts"
24NAME=denyhosts
25DAEMON=/usr/bin/python
26DAEMONCTL=/usr/share/denyhosts/denyhosts_ctl.py
27PIDFILE=/var/run/$NAME.pid
28SCRIPTNAME=/etc/init.d/$NAME
29CONFIG=/etc/denyhosts.conf
30FLAGS="--purge --config=$CONFIG"
31
32# Function that starts the daemon/service.
33d_start() {
34	# Gracefully exit if the package has been removed.
35	test -x $DAEMON || exit 5
36	test -e $CONFIG || (log_failure_msg "Config file doesn't exists!" && log_end_msg 1)
37
38	#check if HOSTS_DENY file exist 
39	HOSTS_DENY=$(grep ^HOSTS_DENY $CONFIG  | cut -d = -f 2)
40	test -e $HOSTS_DENY || touch $HOSTS_DENY
41
42	if [ -e $PIDFILE ]; then
43		pid=$(cat $PIDFILE)
44		if kill -0 "$pid" > /dev/null; then
45			log_success_msg "$DESC already running"
46			return
47		else
48			log_success_msg "Removing stale PID file $PIDFILE."
49			rm -f $PIDFILE
50		fi
51	fi
52	log_daemon_msg "Starting $DESC" "$NAME"
53	start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --startas $DAEMONCTL -- start $FLAGS >/dev/null
54	log_end_msg $?
55}
56
57# Function that stops the daemon/service.
58d_stop() {
59	if [ -e $PIDFILE ]; then
60		pid=$(cat $PIDFILE)
61		if kill -0 "$pid" > /dev/null; then
62			log_daemon_msg "Stopping $DESC" "$NAME"
63			start-stop-daemon --stop --quiet --pidfile $PIDFILE
64			log_end_msg $?
65		else
66			log_failure_msg "I can't stop $DESC" "Maybe it's NOT running?"
67			rm -f $PIDFILE
68		fi
69	fi
70}
71
72# Function that sends a SIGHUP to the daemon/service.
73case "$1" in
74  start)
75        d_start
76	;;
77  stop)
78        d_stop
79	;;
80  restart|force-reload)
81	log_daemon_msg "Restarting $DESC"
82	d_stop || /bin/true
83	sleep 1
84	d_start
85	log_daemon_msg "Done"
86	;;
87  *)
88	log_daemon_msg "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
89	exit 3
90	;;
91esac
92
93exit 0