#!/bin/sh
### BEGIN INIT INFO
# Provides: crond crontab
# Default-Start:  2345
# Default-Stop:   016
# Short-Description: run cron daemon
# Description: cron is a standard UNIX program that runs user-specified
#              programs at periodic scheduled times. vixie cron adds a
#              number of features to the basic UNIX cron, including better
#              security and more powerful configuration options.
### END INIT INFO

CROND=/usr/sbin/crond
CONFIG=/etc/sysconfig/crond
NIRTCFG=/usr/local/natinst/bin/nirtcfg

[ -f $CONFIG ] || exit 1
[ -x $CROND ] || exit 1

. $CONFIG

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

start_cron()
{
    if [ `$NIRTCFG --get section=RTLog,token=disableCron,value=false | tr "[:upper:]" "[:lower:]"` = "false" -o "$1" = "force" ]
    then
        MIN_CRON_STACK=1024
        OLD_STACK_SIZE=`ulimit -s`
        [ $OLD_STACK_SIZE -lt $MIN_CRON_STACK ] && ulimit -s $MIN_CRON_STACK
        echo -n "Starting crond: "
        start-stop-daemon --start --quiet --exec $CROND -- $CRONDARGS
        RETVAL=$?
        ulimit -s $OLD_STACK_SIZE
        if [ $RETVAL -eq 0 ] ; then
            echo "OK"
        else
            echo "FAIL"
        fi
    else
        [ "${VERBOSE}" != "no" ] && echo "Cron disabled from ni-rt.ini"
    fi
}

case "$1" in
  start)
    start_cron
    ;;
  stop)
    echo -n "Stopping crond: "
    start-stop-daemon --stop --quiet --pidfile /var/run/crond.pid
    RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
        echo "OK"
    else
        echo "FAIL"
    fi
    ;;
  status)
    status crond
    exit $?
    ;;
  restart)
    $0 stop && sleep 1 && $0 start
    ;;
  *)
    echo "Usage: /etc/init.d/crond {start|stop|status|restart}"
    exit 1
esac

exit 0

