linux - [Solved-5 Solutions] Are there any standard exit status codes in Linux ? - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

Are there any standard exit status codes in Linux ?

Linux - Solution 1:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>

int main() {
    int status;

    pid_t child = fork();
    if (child <= 0)
        exit(42);
    waitpid(child, &status, 0);
    if (WIFEXITED(status))
        printf("first child exited with %u\n", WEXITSTATUS(status));
    /* prints: "first child exited with 42" */

    child = fork();
    if (child <= 0)
        kill(getpid(), SIGSEGV);
    waitpid(child, &status, 0);
    if (WIFSIGNALED(status))
        printf("second child died with %u\n", WTERMSIG(status));
    /* prints: "second child died with 11" */
}
click below button to copy the code. By - Linux tutorial - team

The shell only stores an 8-bit return code, but sets the high bit if the process was abnormally terminated.

$ sh -c 'exit 42'; echo $?
42
$ sh -c 'kill -SEGV $$'; echo $?
Segmentation fault
139
$ expr 139 - 128
11
click below button to copy the code. By - Linux tutorial - team

If you're seeing anything other than this, then the program probably has a SIGSEGV signal handler which then calls exit normally, so it isn't actually getting killed by the signal.

Linux - Solution 2:

Part 1: Advanced Bash Scripting

  • 1: Catchall for general errors
  • 2: Misuse of shell builtins (according to Bash documentation)
  • 126: Command invoked cannot execute
  • 127: "command not found"
  • 128: Invalid argument to exit
  • 128+n: Fatal error signal "n"
  • 255: Exit status out of range

Part 2: sysexits.h

The ABSG references sysexits.h.

On Linux:

$ find /usr -name sysexits.h
/usr/include/sysexits.h
$ cat /usr/include/sysexits.h

/*
 * Copyright (c) 1987, 1993
 *  The Regents of the University of California.  All rights reserved.

 (A whole bunch of text left out.)

#define EX_OK           0       /* successful termination */
#define EX__BASE        64      /* base value for error messages */
#define EX_USAGE        64      /* command line usage error */
#define EX_DATAERR      65      /* data format error */
#define EX_NOINPUT      66      /* cannot open input */    
#define EX_NOUSER       67      /* addressee unknown */    
#define EX_NOHOST       68      /* host name unknown */
#define EX_UNAVAILABLE  69      /* service unavailable */
#define EX_SOFTWARE     70      /* internal software error */
#define EX_OSERR        71      /* system error (e.g., can't fork) */
#define EX_OSFILE       72      /* critical OS file missing */
#define EX_CANTCREAT    73      /* can't create (user) output file */
#define EX_IOERR        74      /* input/output error */
#define EX_TEMPFAIL     75      /* temp failure; user is invited to retry */
#define EX_PROTOCOL     76      /* remote error in protocol */
#define EX_NOPERM       77      /* permission denied */
#define EX_CONFIG       78      /* configuration error */

#define EX__MAX 78      /* maximum listed value */
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 3:

  • 1 Catchall for general errors
  • 2 Misuse of shell builtins (according to Bash documentation)
  • 126 Command invoked cannot execute
  • 127 "command not found"
  • 128 Invalid argument to exit
  • 128+n Fatal error signal "n"
  • 130 Script terminated by Control-C
  • 255 Exit status out of range
  • This is for bash. However, for other applications, there are different exit codes.

Linux - Solution 4:

From the OpenBSD:

  • According to style(9), it is not good practice to call exit(3) with arbi- trary values to indicate a failure condition when ending a program.
  • In- stead, the pre-defined exit codes from sysexits should be used, so the caller of the process can get a rough estimation about the failure class without looking up the source code

Linux - Solution 5:

  • There are no standard exit codes, aside from 0 meaning success. Non-zero doesn't necessarily mean failure either.
  • stdlib.h does define EXIT_FAILURE as 1 and EXIT_SUCCESS as 0, but that's about it.
  • The 11 on segfault is interesting, as 11 is the signal number that the kernel uses to kill the process in the event of a segfault.
  • There is likely some mechanism, either in the kernel or in the shell, that translates that into the exit code

Related Searches to - linux - linux tutorial - Are there any standard exit status codes in Linux?