Linux program exit status

Program exit status conventions

What is conventional return values for applications in Windows and GNU/Linux respectivly. 0 means success. But what should be used on user-requested abort. When I abort on Windows, it returns 3, but this value is not in the list of system error codes if it is not an ERROR_PATH_NOT_FOUND. GNU binutils uses 1. From a user perspective, returning GetLastError or errno would be good since they are documented, but is these only seems to cover lower level status codes. I am looking for a value that represents «Application terminated unsuccessfully» The reason I wounder is that I want to

from a signal handler that catches some Access Violation/SIGSEGV (i.e programming errors) after printing a message about where it occured. Then the error code should be destiguishable from user input errors.

2 Answers 2

This might help, http://tldp.org/LDP/abs/html/exitcodes.html those are the standard exit code. The rest of them I think are program dependant. basically you need to verify the documentation of the specific software you are looking for. As @devnull said, any exit code that is not zero implies an unsuccessful termination

For completeness, exit code 125 means «not the error we are looking for» to git-bisect. It should be perfectly fine to misuse this value for any other purpose — it only matters in one-off scripts running under git-bisect, for seeking out a specific error, and they must control their exit status tightly anyway.

This is only few conventions about exit codes. Let’s see what some manuals say:

The GNU C Library Reference Manual

  1. There are conventions for what sorts of status values certain programs should return. The most common convention is simply 0 for success and 1 for failure .
  2. A general convention reserves status values 128 and up for special purposes
  3. Some non-POSIX systems use different conventions for exit status values
  4. For greater portability, you can use the macros EXIT_SUCCESS and EXIT_FAILURE for the conventional status value for success and failure.

ISO/IEC 9899:2011 (C11 standard)

If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

That means if you want (and for most cases that’s enough) just indicate success or failure you definitely should use EXIT_SUCCESS and EXIT_FAILURE. If you want indicate other errors, you should reinvent your own exit statuses. For example:

#define HEX_FILE_CREATE 2 #define HEX_FILE_CREATE 3 . 

There is additional tips on what and how you should return:

  1. Warning: Don’t try to use the number of errors as the exit status. This is actually not very useful; a parent process would generally not care how many errors occurred. Worse than that, it does not work, because the status value is truncated to eight bits. Thus, if the program tried to report 256 errors, the parent would receive a report of 0 errors—that is, success
  2. For the same reason, it does not work to use the value of errno as the exit status—these can exceed 255
  1. For success always use EXIT_SUCCESS
  2. Your failure exit status should be beetween 1 and 127
  3. Don’t use errno error code as exit status
Читайте также:  What is checksum in linux

Источник

What are Exit Codes in Linux?

Unraveling the mystery of exit codes in Linux. Learn what are the exit codes and why and how they are used.

An exit code or exit status tells us about the status of the last executed command. Whether the command was completed successfully or ended with an error. This is obtained after the command terminates. The basic ideology is that programs return the exit code 0 to indicate that it executed successfully without issues. Code 1 or anything other than 0 is considered unsuccessful. There are many more exit codes other than 0 and 1, which I’ll cover in this article.

Various exit codes in Linux shell

Let us take a quick look at the prominent exit codes in the Linux shell:

Exit code Meaning of the code
0 Command executed with no errors
1 Code for generic errors
2 Incorrect command (or argument) usage
126 Permission denied (or) unable to execute
127 Command not found, or PATH error
128+n Command terminated externally by passing signals, or it encountered a fatal error
130 Termination by Ctrl+C or SIGINT (termination code 2 or keyboard interrupt)
143 Termination by SIGTERM (default termination)
255/* Exit code exceeded the range 0-255, hence wrapped up

The termination signals like 130 (SIGINT or ^C ) and 143 (SIGTERM) are prominent, which are just 128+n signals with n standing for the termination code.

Retrieving the exit code

The exit code of the previously executed command is stored in the special variable $? . You can retrieve the exit status by running:

This will be used in all our demonstrations to retrieve the exit code. Note that the exit command supports carrying the same exit code of the previous command executed.

Exit code 0

Exit code 0 means that the command is executed without errors. This is ideally the best case for the completion of commands. For example, let us run a basic command like this

This exit code 0 means that the particular command was executed successfully, nothing more or less. Let us demonstrate some more examples. You may try killing a process; it will also return the code 0 .

Viewing a file’s contents will also return an exit code 0, which implies only that the ‘cat’ command executed successfully.

Читайте также:  Все оболочки линукс минт

Exit code 1

Exit code 1 is also a common one. It generally means the command terminated with a generic error. For example, using the package manager without sudo permissions results in code 1. In Arch Linux, if I try this:

exit code 1 (impermissible operation resulted in this code)

It will give me exist code as 1 meaning the last command resulted in error.

If you try this in Ubuntu-based distros ( apt update without sudo), you get 100 as an error code for running ‘apt’ without permissions. This is not a standardized error code, but one specific to apt.

Division by zero results in code 1

While this is a general understanding, we can also interpret this as «operation impermissible». Operations like dividing by zero also result in code 1.

Exit code 2

This exit code is given out when the command executed has a syntax error. Misusing the arguments of commands also results in this error. It generally suggests that the command could not execute due to incorrect usage. For example, I added two hyphens to an option that’s supposed to have one hyphen. Code 2 was given out.

Invalid argument resulted in exit code 2When permission is denied, like accessing the /root folder, you get error code 2. Permission denied gives out code 2

Exit code 126

126 is a peculiar exit code since it is used to indicate a command or script was not executed due to a permission error. This error can be found when you try executing a shell script without giving execution permissions. Note that this exit code appears only for the ‘execution‘ of scripts/commands without sufficient permissions, which is different from a generic Permission Denied error. So, on’t confuse it with the previous example you saw with exit code 2. There, ls command ran and the permission issue came with the directory it was trying to execute. Here, the permission issues came from the script itself.

Exit code 127

This is another common one. Exit code 127 refers to «command not found». It usually occurs when there’s a typo in the command executed or the required executable is not in the $PATH variable. For example, I often see this error when I try executing a script without its path. Script executed without the path gives Or when the executable file you’re trying to run, is not listed in the $PATH variable. You can rectify this by adding the parent directory to the PATH variable. You’ll also get this exit code when you type commands that do not exist. Unmount is not a command, and Screenfetch is not installed, which resulted in code 127

Exit code series 128+n

When an application or command is terminated or its execution fails due to a fatal error, the adjacent code to 128 is produced (128+n), where n is the signal number. This includes all types of termination codes, like SIGTERM , SIGKILL , etc that apply to the value ‘n’ here.

Code 130 or SIGINT

SIGINT or Signal for Keyboard Interrupt is induced by interrupting the process by termination signal 2, or by Ctrl+C. Since the termination signal is 2, we get a code 130 (128+2). Here’s a video demonstrating the interrupt signal for lxappearance .

Читайте также:  How to copy files to usb in linux

Note that these signals may not appear if terminated from the same session from which the process was started. If you’re reproducing these, terminate from a different shell.

On a personal note, signal 128 was impossible to reproduce.

What if the code exceeds 255?

Recent versions of Bash retain the original exit code value even beyond 255, but generally, if the code exceeds 255, then it is wrapped up. That is, code 256 becomes ‘0’, 257 becomes ‘1’, 383 becomes ‘127’, and so on and so forth. To ensure better compatibility, keep the exit codes between 0 and 255.

Wrapping up

I hope you learned something about the exit codes in the Linux shell. Using them can come in handy for troubleshooting various issues. If you are using these codes in a shell script, make sure you understand the meaning of each code in order to make it easier for troubleshooting. In case you need a reference, check out the Bash series here: That’s all about the article. Feel free to let me know in the comments section if I have missed anything.

Источник

Linux program exit status

The exit status of an executed command is the value returned by the waitpid system call or equivalent function. Exit statuses fall between 0 and 255, though, as explained below, the shell may use values above 125 specially. Exit statuses from shell builtins and compound commands are also limited to this range. Under certain circumstances, the shell will use special values to indicate specific failure modes.

For the shell’s purposes, a command which exits with a zero exit status has succeeded. A non-zero exit status indicates failure. This seemingly counter-intuitive scheme is used so there is one well-defined way to indicate success and a variety of ways to indicate various failure modes. When a command terminates on a fatal signal whose number is N , Bash uses the value 128+ N as the exit status.

If a command is not found, the child process created to execute it returns a status of 127. If a command is found but is not executable, the return status is 126.

If a command fails because of an error during expansion or redirection, the exit status is greater than zero.

The exit status is used by the Bash conditional commands (see Conditional Constructs) and some of the list constructs (see Lists of Commands).

All of the Bash builtins return an exit status of zero if they succeed and a non-zero status on failure, so they may be used by the conditional and list constructs. All builtins return an exit status of 2 to indicate incorrect usage, generally invalid options or missing arguments.

The exit status of the last command is available in the special parameter $? (see Special Parameters).

Источник

Оцените статью
Adblock
detector