PDA

View Full Version : Writing your own rc.d script - some syntax questions



akvino
9th August 2007, 08:44 PM
#!/bin/bash
#
# postfix Postfix Mail Transfer Agent
#
# chkconfig: 2345 80 30 //QUESTION -does kernel read this just as #!/bin/bash
# description: Postfix is a Mail Transport Agent, which is the program \
# that moves mail from one machine to another.
# processname: master
# pidfile: /var/spool/postfix/pid/master.pid
# config: /etc/postfix/main.cf
# config: /etc/postfix/master.cf
#
# Based on startup script from Simon J Mudd < sjmudd@pobox.com>
# 25/02/99: Mostly s/sendmail/postfix/g by John A. Martin <jam@jamux.com>
# 23/11/00: Changes & suggestions by Ajay Ramaswamy < ajayr@bigfoot.com>
# 20/01/01: Changes to fall in line with RedHat 7.0 style
# 23/02/01: Fix a few untidy problems with help from Daniel Roesen.

# Source function library.
. /etc/rc.d/init.d/functions /QUESTION - what does 'functions' do

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0 /QUESTION - this is checking global variable? yes? othervise WHAT IS exit 0 [B]

[ -x /usr/sbin/postfix ] || exit 0 [B]/QUESTION - someone pls explain following 3 lines
[ -d /etc/postfix ] || exit 0
[ -d /var/spool/postfix ] || exit 0

RETVAL=0
prog="postfix"

start() {
# Start daemons.
echo -n $"Starting postfix: "
/usr/bin/newaliases >/dev/null 2>&1
/usr/sbin/postfix start 2>/dev/null 1>&2 && success || failure $"$prog start"
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/postfix
echo
return $RETVAL
}

stop() {
# Stop daemons.
echo -n $"Shutting down postfix: "
/usr/sbin/postfix stop 2>/dev/null 1>&2 && success || failure $"$prog stop"
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/postfix
echo
return $RETVAL
}

reload() {
echo -n $"Reloading postfix: "
/usr/sbin/postfix reload 2>/dev/null 1>&2 && success || failure $"$prog reload"
RETVAL=$?
echo
return $RETVAL
}

abort() {
/usr/sbin/postfix abort 2>/dev/null 1>&2 && success || failure $"$prog abort"
return $?
}

flush() {
/usr/sbin/postfix flush 2>/dev/null 1>&2 && success || failure $"$prog flush"
return $?
}

check() {
/usr/sbin/postfix check 2>/dev/null 1>&2 && success || failure $"$prog check"
return $?
}

restart() {
stop
start
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
reload)
reload
;;
abort)
abort
;;
flush)
flush
;;
check)
check
;;
status)
status master
;;
condrestart)
[ -f /var/lock/subsys/postfix ] && restart || :
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|abort|flush|check|statu s|condrestart}"
exit 1
esac

exit $?
------------------------------------------------------------------------

This is the syntax for postfix

markkuk
9th August 2007, 09:37 PM
# chkconfig: 2345 80 30 //QUESTION -does kernel read this just as #!/bin/bash
It's a bash comment, and the kernel never sees it. Bash just skips it. The "chkconfig" program uses it to set up the links to start and stop the service when the script is installed.



# Source function library.
. /etc/rc.d/init.d/functions /QUESTION - what does 'functions' do
It defines various bash functions that may be used in rc.d scripts. See the source.



# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0 /QUESTION - this is checking global variable? yes? othervise WHAT IS exit 0 [B]

$NETWORKING is an environment variable defined in /etc/sysconfig/network. There are no global variables in bash. If the value of the variable equals "no", then the script exits with status 0 (success).


[ -x /usr/sbin/postfix ] || exit 0 [B]/QUESTION - someone pls explain following 3 lines
[ -d /etc/postfix ] || exit 0
[ -d /var/spool/postfix ] || exit 0

"[" is an alias for the command "test". See "man test". The script tests for the existence of necessary programs and directories and quits if they are missing.