Exit error codes 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.

Читайте также:  What is binary file in linux

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 .

Читайте также:  Google disk linux manjaro

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.

Источник

Standard Exit Status Codes in Linux

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

In Linux, when a process is terminated, it returns an exit code. Upon successful execution, this code is equal to zero. Any non-zero exit code indicates that some error occurred.

Although we’re free to choose what exit codes we use in our scripts and programs, there are some standard codes that have a special meaning. In this tutorial, we’ll have a look at these codes and what they mean.

2. Exit Codes

In shell scripts, we use the exit command to generate exit codes. By using different exit codes for different errors, we can give an indication of what went wrong.

The exit command takes one argument, the exit code we want to use. For example:

As we’ve already learned from the overview, some exit codes have special meanings. However, this doesn’t mean we can’t use them. They’re special, not reserved.

Читайте также:  Dr web desktop security suite linux

Also, some special codes might have different meanings in different shells. The examples in this article apply to the Bash shell.

3. Special Exit Codes

Let’s have a look at some special codes.

3.1. General Error: 1

This is the most used exit code and should be used as a catch-all value for miscellaneous errors.

3.2. Misuse of Shell Built-in: 2

Exit code 2 signifies invalid usage of some shell built-in command. Examples of built-in commands include alias, echo, and printf.

3.3. Cannot Execute: 126

In this case, the command invoked can’t be executed. This will most likely occur when there’s a permission problem or the command isn’t executable.

3.4. Command Not Found: 127

A command couldn’t be found. This may happen, for example, because there was a typo or an issue with our PATH.

3.5. Invalid Argument To Exit: 128

The exit command only takes a positive integer as its argument. This means any negative, fractional, or non-numeric values aren’t allowed.

3.6. Fatal Error Signal ‘n‘: 128+n

In Linux, programs might send one of 33 different signals. When a program terminates after receiving one of these signals, it will return an error code equal to 128 + signal-number.

For example, when we terminate a program by typing Control-C, we’re effectively sending it a SIGINT signal. This signal has a value of 2, therefore, the program will stop its execution and return an exit code with a value 128 + 2 = 130.

3.7. Exit Status Out of Range: 255

Depending on our shell, exit code 255 might mean that the returned exit code is outside of the 0-255 range.

4. Exit Codes Above 255

Because exit codes are represented by a single byte value, the highest possible exit code is 255. However, nothing prevents us from returning exit codes larger than this. Values over 255 are out of range and get wrapped around.

We should be very careful as this can be a source of unexpected results. For example, the exit code of 383 will be wrapped around and result in an effective exit code of 127, which translates to “command not found”:

$ bash -c "exit 383" echo $? 127

While it’s allowed to return exit values over 255, it’s better to avoid it at all times.

5. Conclusion

In this article, we’ve learned which exit codes should be treated as reserved and when we can use them in the scripts and programs we write. Some of them we should never use as it would be very confusing for our users.

We have to remember that different shells might have different special exit codes. This is something to be aware of.

Also, when writing programs or scripts, we should document the exit codes we use as a courtesy to our users.

Источник

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