Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing System V init scripts 1

Status
Not open for further replies.

nix45

MIS
Nov 21, 2002
478
US
I'm writing a System V init script for a custom application. I'm basically just reverse engineering an existing script. I have a question on the Postfix init script on RHEL AS 3.0. I'm just using Postfix as an example, a lot of them do the same thing. I know Perl scripting much better than bash scripting.
Code:
start() {
        # Start daemons.
        echo -n "Starting postfix: "
        /usr/sbin/postalias /etc/postfix/aliases
        /usr/sbin/postfix start 2>/dev/null 1>&2 && success || failure
        RETVAL=$?
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/postfix
        echo
        return $RETVAL
}

In the line "/usr/bin/postfix start 2>/dev/null 1>&2 && success || failure", what does the "&& success || failure" mean? I know that && means to only execute if the previous command was successful and I know that || means "or", but whats with the 'success' and 'failure'? They aren't system commands or anything.

At the end where it says...
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/postfix
echo
return $RETVAL

...they are saying "if the return value is 0, then create the file /var/lock/subsys/postfix". Why do they create this file? It doesn't seem to serve any purpose. Also, it then says "echo" on a line by itself, and then "return $RETVAL". What is the purpose of that?

Thanks,
Chris
 
The "success" and "failure are functions called to display status. In the original postfix script, look for a line like:

. /etc/init.d/functions

This loads some generic functions , like those that show "OK" or "FAILED" in green & red when Linux starts.

The /var/lock/subsys/postfix file is used by the script to determine if postfix is running. There's a startup option "condrestart" which only works if it's running, and has the line:

[ -f /var/lock/subsys/postfix ] && restart || :

which says "if this file exists, call the restart() function". I'm not sure about the ":" in "|| :", though.

I think whether you need this depends on your script. I don't know if there is a rule of thumb or standard for creating these files.

I'm no expert, so this reflects my interpretation in simple terms that even I can understand... :)

 
thanks dude....anyone know about the last part? I know that in a Perl script, if you have a subroutine you might end it with something like "return $variable". Is that along the same lines?

Thanks,
Chris
 
Oops, I missed that part. Yes, that's what it'll do- it's the return value for the start() function.

i.e.:
testfunc()
{ return 5 }

statuscode=`testfunc`
exit $statuscode

Then at the OS prompt:
linux# ./testscript
linux# echo $?
5
linux#

$? is kind of magic- it's the result of the last operation. So here, "exit $?" would do the same thing. That's why RETVAL is used to capture $? in the postfix script. Otherwise, $? keeps changing.
 
What part of the script reads the return value though? I know that if $? is equal to 0 the the last command was a success, but where does it read this variable? Do the success and failure functions use it? It seems like the command will run just fine without the "return $RETVAL" at the end. Thats why its confusing me a bit.
 
It would be just fine. Continuing to use "postfix" as an example, the very last line of the script is "exit $?". So this value could be used by a script either calling "postfix" or run right after it.

The return value isn't required if you don't need it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top