Merge pull request #29686 from lkxed/20230709-0-What-are-Exit-Codes-in-Linux

[手动选题][tech]: 20230709.0 ️ What are Exit Codes in Linux.md
This commit is contained in:
Xingyu.Wang 2023-07-11 16:47:34 +08:00 committed by GitHub
commit e351b1c106
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,220 @@
[#]: subject: "What are Exit Codes in Linux?"
[#]: via: "https://itsfoss.com/linux-exit-codes/"
[#]: author: "Pranav Krishna https://itsfoss.com/author/pranav/"
[#]: collector: "lkxed"
[#]: translator: " "
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
What are Exit Codes in Linux?
======
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.
Now that you are briefly familiar with the exit codes let's see about their usage.
### Retrieving the exit code
The exit code of the previously executed command is stored in the [special variable][1] `$?`. You can retrieve the exit status by running:
```
echo $?
```
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
```
neofetch
echo $?
```
![][2]
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][3]; it will also return the code `0`.
```
pkill lxappearance
```
![][4]
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][5] without sudo permissions results in code 1. In Arch Linux, if I try this:
```
pacman -Sy
```
It will give me exist code as 1 meaning the last command resulted in error.
![exit code 1 (impermissible operation resulted in this code)][6]
> 📋 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.
While this is a general understanding, we can also interpret this as "operation impermissible".
Operations like dividing by zero also result in code 1.
![Division by zero results in code 1][7]
### 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.
```
grep --z file.txt
```
![Invalid argument resulted in exit code 2][8]
When permission is denied, like accessing the /root folder, you get error code 2.
![Permission denied gives out code 2][9]
### 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.
![][10]
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][11]". 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 "command not found" or code 127][12]
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][13].
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][14]
### 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 **Sig**nal for Keyboard **Int**errupt 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`.
<video src="https:/itsfoss.com/content/media/2023/06/exit_code_128-n-SIGINT-_compressed.mp4">
#### Code 137 or SIGKILL
The `SIGKILL` termination **sig**nal that **kill**s the process instantly has a termination signal 9. This is the last method one should use while terminating an application.
The exit code thrown is 137 since the termination signal is 9 (128+9).
<video src="https://itsfoss.com/content/media/2023/06/exit_code_128-n-SIGKILL-_compressed.mp4">
#### Code 143 or SIGTERM
`SIGTERM` or **Sig**nal to **Term**inate is the default behavior when a process is killed without specifying arguments.
The termination code for SIGTERM is 15, hence this signal gets an exit code of 143 (128+15).
<video src="https://itsfoss.com/content/media/2023/06/exit_code_128-n-SIGTERM-_compressed-1.mp4">
There are other termination signals that you may not have known before; they too have their own exit codes similar to these. You can check them out here:
> 📋 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.
--------------------------------------------------------------------------------
via: https://itsfoss.com/linux-exit-codes/
作者:[Pranav Krishna][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/pranav/
[b]: https://github.com/lkxed/
[1]: https://linuxhandbook.com:443/bash-special-variables/
[2]: https://itsfoss.com/content/images/2023/06/exit_code_0.png
[3]: https://itsfoss.com/how-to-find-the-process-id-of-a-program-and-kill-it-quick-tip/
[4]: https://itsfoss.com/content/images/2023/06/exit_code_0.gif
[5]: https://itsfoss.com/package-manager/
[6]: https://itsfoss.com/content/images/2023/06/exit_code_1.png
[7]: https://itsfoss.com/content/images/2023/06/exit_code_1-division_by_0-.png
[8]: https://itsfoss.com/content/images/2023/06/exit_status_2-misusing_arguments--1.png
[9]: https://itsfoss.com/content/images/2023/06/exit_code_2-permission_denied-.png
[10]: https://itsfoss.com/content/images/2023/06/exit_code_126.png
[11]: https://itsfoss.com/bash-command-not-found/
[12]: https://itsfoss.com/content/images/2023/06/exit_code_127.png
[13]: https://itsfoss.com/add-directory-to-path-linux/
[14]: https://itsfoss.com/content/images/2023/06/exit_code_127-command_not_found--1.png
[def]: ttps://itsfoss.com/content/media/2023/06/exit_code_128-n-SIGINT-_compressed.mp