[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#562506: init scripts should not use set -e



On Tue, 01 Jun 2010, Russ Allbery wrote:
> Here is proposed wording, which hopefully reflects the subsequent
> discussion.  I'm looking for seconds.
> 
[...]
>  	  <p>
> +	    Be careful of using <tt>set -e</tt> in <file>init.d</file>
> +	    scripts.  Writing correct <file>init.d</file> scripts requires
> +	    accepting various error exit statuses when daemons are already
> +	    running or already stopped without aborting
> +	    the <file>init.d</file> script, and common <file>init.d</file>
> +	    function libraries are not safe to call with <tt>set -e</tt>
> +	    in effect<footnote>
> +	      <tt>/lib/lsb/init-functions</tt>, which assists in writing
> +	      LSB-compliant init scripts, may fail if <tt>set -e</tt> is
> +	      in effect and echoing status messages to the console fails,
> +	      for example.
> +	    </footnote>.  For <tt>init.d</tt> scripts, it's often easier
> +	    to not use <tt>set -e</tt> and instead check the result of
> +	    each command separately.
> +	  </p>
> +
> +	  <p>
>  	    If a service reloads its configuration automatically (as
>  	    in the case of <prgn>cron</prgn>, for example), the
>  	    <tt>reload</tt> option of the <file>init.d</file> script
> @@ -7158,13 +7175,19 @@ strip --strip-unneeded <var>your-lib</var>
>            language currently used to implement it.
>          </p>
>  	<p>
> -	  Shell scripts (<prgn>sh</prgn> and <prgn>bash</prgn>)
> -	  should almost certainly start with <tt>set -e</tt> so that
> -	  errors are detected.  Every script should use
> -	  <tt>set -e</tt> or check the exit status of <em>every</em>
> -	  command.
> +	  Shell scripts (<prgn>sh</prgn> and <prgn>bash</prgn>) other than
> +	  <file>init.d</file> scripts should almost certainly start
> +	  with <tt>set -e</tt> so that errors are detected.
> +	  <file>init.d</file> scripts are something of a special case, due
> +	  to how frequently they need to call commands that are allowed to
> +	  fail, and it may instead be easier to check the exit status of
> +	  commands directly.  See <ref id="writing-init"> for more
> +	  information about writing <file>init.d</file> scripts.
> +	</p>
> +	<p>
> +	  Every script should use <tt>set -e</tt> or check the exit status
> +	  of <em>every</em> command.
>  	</p>
> -
>  	<p>
>  	  Scripts may assume that <file>/bin/sh</file> implements the
>  	  SUSv3 Shell Command Language<footnote>

Seconded.

Cheers,
-- 
Raphaël Hertzog

Like what I do? Sponsor me: http://ouaza.com/wp/2010/01/05/5-years-of-freexian/
My Debian goals: http://ouaza.com/wp/2010/01/09/debian-related-goals-for-2010/

Attachment: signature.asc
Description: Digital signature


Reply to: