Sysexits

module

sysexits

Exit status codes for system programs.

Usage

To support running on a Mac running OS X 10.7 or later, you’ll need to force the gem first in the load path to avoid Apple’s own helpfully vendored ‘sysexits’ library:

gem 'sysexits'
require 'sysexits'

You can look up the appropriate code yourself, and exit using the regular exit method, of course:

status_code = Sysexits.exit_status( :success )
exit( status_code )

Or, just use Sysexits::exit with the code name:

Sysexits.exit( :usage )

Or, mix the enhanced ::exit into your namespace:

include Sysexits
exit( :unavailable )

It also supports exit! in all the same ways as above.

License

This file was derived almost entirely from the BSD sysexits.h, which is distributed under the following license:

Copyright © 1987, 1993 The Regents of the University of California. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. All advertising materials mentioning features or use of this software must display the following acknowledgement:

    This product includes software developed by the University of
    California, Berkeley and its contributors.
  4. Neither the name of the University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Constants

EX_CANTCREAT

:cantcreat, :cant_create_output - A (user specified) output file cannot be created.

EX_CONFIG

:config, :config_error - There was an error in a user-specified configuration value.

EX_DATAERR

:dataerr, :data_error - The input data was incorrect in some way. This should only be used for user data, not system files.

EX_IOERR

:ioerr - An error occurred while doing I/O on a file.

EX_NOHOST

:nohost, :no_such_host - The host specified did not exist. This is used in mail addresses or network requests.

EX_NOINPUT

:noinput, :input_missing - An input file (not a system file) did not exist or was not readable. This could also include errors like "No message" to a mailer (if it cared to catch it).

EX_NOPERM

:noperm, :permission_denied - You did not have sufficient permission to perform the operation. This is not intended for file system problems, which should use NOINPUT or CANTCREAT, but rather for higher level permissions.

EX_NOUSER

:nouser, :no_such_user - The user specified did not exist. This might be used for mail addresses or remote logins.

EX_OK

:ok, :success - Successful termination

EX_OSERR

:oserr, :operating_system_error - An operating system error has been detected. This is intended to be used for such things as "cannot fork", "cannot create pipe", or the like. It includes things like getuid returning a user that does not exist in the passwd file.

EX_OSFILE

:osfile, :operating_system_file_error - Some system file (e.g., /etc/passwd, /etc/utmp, etc.) does not exist, cannot be opened, or has some sort of error (e.g., syntax error).

EX_PROTOCOL

:protocol, :protocol_error - The remote system returned something that was "not possible" during a protocol exchange.

EX_SOFTWARE

:software, :software_error - An internal software error has been detected. This should be limited to non-operating system related errors.

EX_TEMPFAIL

:tempfail, :temporary_failure, :try_again - Temporary failure, indicating something that is not really a serious error. In sendmail, this means that a mailer (e.g.) could not create a connection, and the request should be reattempted later.

EX_UNAVAILABLE

:unavailable, :service_unavailable - A service is unavailable. This can occur if a support program or file does not exist. This can also be used as a catchall message when something you wanted to do doesn't work, but you don't know why.

EX_USAGE

:usage - The command was used incorrectly, e.g., with the wrong number of arguments, a bad flag, a bad syntax in a parameter, or whatever.

EX__BASE

:_base - The base value for sysexit codes

EX__MAX

:_max - The maximum listed value. Automatically determined because, well, we can and I'll forget to update this if I ever add any codes.

REVISION

The library’s revision id

STATUS_CODES

Mapping of human-readable Symbols to statuses

VERSION

The library version

Public Instance Methods

anchor
exit( status=EX_OK )

Exit with the exit status, which can be either one of the keys of STATUS_CODES or a number.

# File lib/sysexits.rb, line 242
def exit( status=EX_OK )
        status = exit_status( status )
        ::Kernel.exit( status )
end
anchor
exit!( status=EX_OK )

Exit with the given status without running exit handlers.

# File lib/sysexits.rb, line 249
def exit!( status=EX_OK )
        status = exit_status( status )
        ::Kernel.exit!( status )
end
anchor
exit_status( status )

Turn status into a numeric exit status and return it.

# File lib/sysexits.rb, line 230
def exit_status( status )
        case status
        when Symbol, String
                return STATUS_CODES[ status.to_sym ] || status
        else
                return status
        end
end