Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do many init.d scripts end in "exit $?"?

I've seen a lot of strange quirks in CentOS 6.5's init.d scripts, but one pattern I've seen at the end of most of these scripts is

case "$1" in
    # ... commands here
esac
exit $?

What is the purpose of "exit $?" here?

like image 584
Score_Under Avatar asked Aug 18 '14 10:08

Score_Under


1 Answers

It makes the script return the return code of the last significant command to the calling init system. Whenever a command exits, its return code is stored on $? by the shell.

It's actually not really necessary to explicitly specify $? but scripters probably just include it to be clear about what it intends to do.

exit: exit [n]

Exit the shell.

Exits the shell with a status of N. If N is omitted, the exit status is that of the last command executed.

I also hope you don't actually mean eend $? of OpenRC:

eend retval [string ]

If retval does not equal 0 then output the string using eerror and !! in square > brackets at the end of the line. Otherwise output ok in square brackets at the end of the line. The value of retval is returned.

See source.

like image 117
konsolebox Avatar answered Sep 20 '22 15:09

konsolebox