#!/bin/sh
#
# crond		Start/Stop the cron clock daemon.
#
# chkconfig:	2345 40 60
# description:	cron is a standard UNIX program that runs user-specified \
#		programs at periodic scheduled times. cronie adds a \
#		number of features to the basic UNIX cron, including better \
#		security and more powerful configuration options.
#
# processname:	crond
# config:	/etc/cron.d
# pidfile:	/var/run/crond.pid


# Source function library.
. /etc/rc.d/init.d/functions

CROND_ARGS=""
PIDFILE="/var/run/crond.pid"
CROND_OOM_ADJUST=-200

# Get service config
[ -f /etc/sysconfig/cron ] && . /etc/sysconfig/cron

adjust_oom() {
    if [ -e $PIDFILE ]; then
	for pid in $(cat $PIDFILE); do
	    echo "$CROND_OOM_ADJUST" 2>/dev/null > /proc/$pid/oom_score_adj
	done
    fi
}

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/crond ]; then
		msg_already_running "cronie crond"
		return
	fi

	if is_yes "$CROND_SYSLOG_RESULT"; then
		CROND_ARGS="$CROND_ARGS -s"
	fi

	if [ -n "$CROND_MAIL_PROG" ]; then
		# XXX: should we handle spaces in $CROND_MAIL_PROG?
		CROND_ARGS="$CROND_ARGS -m $CROND_MAIL_PROG"
	fi

	msg_starting "cronie crond"
	daemon /usr/sbin/crond $CROND_ARGS
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/crond
	adjust_oom
}

stop() {
	if [ ! -f /var/lock/subsys/crond ]; then
		msg_not_running "cronie crond"
		return
	fi

	msg_stopping "cronie crond"
	killproc crond
	rm -f /var/lock/subsys/crond
}

reload() {
	if [ ! -f /var/lock/subsys/crond ]; then
		msg_not_running "cronie crond"
		RETVAL=7
		return
	fi

	msg_reloading "cronie crond"
	killproc crond -HUP
	RETVAL=$?
}

condrestart() {
	if [ ! -f /var/lock/subsys/crond ]; then
		msg_not_running "cronie crond"
		RETVAL=$1
		return
	fi

	stop
	start
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  reload|force-reload|flush-logs)
  	reload
	;;
  status)
	status --pidfile /var/run/crond.pid crond
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|reload|force-reload|flush-logs|status}"
	exit 3
esac

exit $RETVAL
