Tag: Linux

  • crondのスクリプトを汎用化しようとしてみたやつ
#!/bin/bash

PROGFILE="/usr/local/test/program"
PROGNAME=`basename $PROGFILE`
LOCKFILE="/var/lock/subsys/$PROGNAME"
PIDFILE="/var/run/$PROGNAME.pid"
CONFFILE="/etc/sysconfig/$PROGNAME"
PROGARGS=""

. /etc/init.d/functions
[ -e $CONFFILE ] && . $CONFFILE;

start() {
    echo -n $"Starting $PROGNAME: "
    if [ -e $LOCKFILE ]; then
        if [ -e $PIDFILE ] && [ -e /proc/`cat $PIDFILE` ]; then
            echo -n $"cannot start $PROGNAME: $PROGNAME is already running.";
            failure $"cannot start $PROGNAME: $PROGNAME is already running.";
            echo
            return 1
        fi
    fi
    daemon $PROGFILE $PROGARGS
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch $LOCKFILE;
    return $RETVAL
}

stop() {
    echo -n $"Stopping $PROGNAME: "
    if [ ! -e $LOCKFILE ]; then
        echo -n $"cannot stop $PROGNAME: $PROGNAME is not running."
        failure $"cannot stop $PROGNAME: $PROGNAME is not running."
        echo
        return 1;
    fi
    killproc -p $PIDFILE $PROGFILE
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f $LOCKFILE;
    return $RETVAL
}

rhstatus() {
    status -p $PIDFILE $PROGFILE
}

restart() {
    stop
    start
}

reload() {
    echo -n $"Reloading $PROGNAME daemon configuration: "
    killproc -p $PIDFILE $PROGFILE -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  reload)
    reload
    ;;
  status)
    rhstatus
    ;;
  condrestart)
    [ -f $LOCKFILE ] && restart || :
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|reload|restart|condrestart}"
    exit 1
esac

Last-modified: 2012-08-23 (木) 14:48:09 (1827d)