How to Use Linux Exit Codes to Troubleshoot Your Linux System

by Jonathan Mann
10 minutes read

How to Use Linux Exit Codes to Troubleshoot Your Linux System


You Can Troubleshoot Faster By means of Linux Exit Codes

Exit codes, in a comparable means granted as rejoinder codes or run away ailments, are numerical signals that a regimen, command, or script sends previously to the operating system (or terming process) once it coverings running. You don’t have to dig using possibly lengthy log documents to diagnose a inability – an run away code oftentimes lends a rapid idea about the nature of the woe.

The universally welcomed convention is that once a regimen runs successfully, it antiphons an run away code of 0. Any kind of other digit, commonly within the criterion selection of 1 to 255, training courses that something went dishonorable.

The most inalienable means to check the run away code of the last enforced foreground command in Unix-like shells (such as Revelry or Zsh) is by checking the distinctive shuck variable $?. For example, first rushed any command and then check its run away code:

ls /nonexistent_directory
echo $?
Shwoing Exit Codes Of Last Implemented Command
How to Use Linux Exit Codes to Troubleshoot Your Linux System 8

The resemble $? command displays the numerical run away code stored in the $? variable. If you appointment 0, it intends the previous command finalized successfully. A non-zero merit like 1, 2, or 127 training courses an blunder, and the fussy digit offers a idea about the kind of woe — perhaps a lacking out on record, a lacking catalog, or disadvantageous sanctions.

Debugging Your Script By means of Exit Codes

Exit codes are specifically wonderful once you’re concocting shuck scripts. They help your script detect whether something went relevant or dishonorable, permitting it to establish what to execute next.

Here’s a simplistic example that xeroxes a record to a previously-up folder and records whether the xerox was effective:

#!/bin/bash
# Try to copy the file
cp important_file.txt backup/

# Check if the copy was successful using the exit code ($? holds the exit code from the last command)
if [ $? -eq 0 ]; then
echo "Backup successful!"
else
echo "Backup failed with exit code $?"
exit 1 # Exit the script with an error code to indicate something went wrong.
fi

Once the cp command runs, it antiphons an run away code. An run away code of 0 intends whatever went efficiently. If the code is not 0, something went dishonorable – and the script prints an blunder article and leaves using a code of 1.

Ensconcing Your Own Exit Codes

In renovation to surveying the run away codes from commands, you can display your own run away codes to confirm opposite species of dilemmas. This practice makes your scripts more educational, permitting any individual gaining gain serviceability of of them to come to be aware exactly what sort of woe arised.

Ponder this script:

#!/bin/bash
# Verify that an argument (filename) is provided.
if [ -z "$1" ]; then
echo "Usage: $0 "
exit 1 # Exit code 1 indicates that no argument was provided.
fi

# Check if the file provided as the first argument exists.
if [ ! -f "$1" ]; then
echo "Error: File not found"
exit 2 # Custom exit code 2 indicates that the file was not found.
fi

If a person runs your script, and it falls short, they can refer to the run away code to detect the details blunder. In this example, run away code 1 signals that the borrower did not lug out a pertinent discussion, while run away code 2 clearly training courses that the record wasn’t established. This structured strategy is specifically wonderful once your script is room of a bigger system or once another script trusts its result.

List of Typical Exit Codes

While opposite regimen can display their own run away codes, there are some average crazes you’ll appointment across many command-jabber upward devices – specifically in Linux and Unix-like devices. Here are some of the most famously run into:

  • 0: Every little thing’s nice! The regimen ran without any heartache.
  • 1: Something went dishonorable, but the blunder isn’t fussy. This is like the catch-all of run away codes.
  • 2: Maltreatment of shuck builtins. This can happen if you gain serviceability of a command falsely, such as forgetting pertinent criteria.
  • 126: The command exists, but you don’t have assent to rushed it.
  • 127: Command not established. This intends the command doesn’t exist or isn’t in the system’s course. Double-check your spelling.
  • 128: Illegitimate run away discussion. You’ve offered a digit that doesn’t gain sense as an run away code.
  • 130: Reign stopped by Ctrl + C. This takes place once you by hand eschew a regimen.
  • 137: Out-of-retrospect (OOM) standing.
  • 255: The regimen dared to rejoinder an run away code external the credible selection.

Some fussy regimen have their own incomparable run away codes. For example, grep antiphons 0 once it spots complements, 1 once it doesn’t spot any complements, and 2 once there’s an blunder. Also, designers can practically gain serviceability of practically any digit (commonly 0 to 255 on Unix-like devices).

Chaining Commands Based on Success or Inability

Revelry in a comparable means lends two handy operators && and || that permit you string commands together depending on whether the previous one prospered or failed:

  • && (AND catalyst): Runs the next command only if the previous one prospered (run away code 0).
  • || (OR catalyst): Runs the next command only if the previous one failed (non-zero run away code).

For example, mean you want to model a catalog and then sweetly reclamation relevant into it. If amassing the catalog falls short or editing relevant into it doesn’t job, you can want to brandish an blunder article:

mkdir new_directory && cd new_directory || echo "Failed to create and access directory"

Here, mkdir new_directory shots to model the folder. If that works, the code sweetly runs cd new_directory. If either relocation falls short, the || catalyst kicks in and prints the blunder article.

Last Ideas

Exit codes can seem like a miniscule information, but they’re room of what makes command-jabber upward devices so strong. They model a average language for regimen to convey about victories and inability, which is pertinent for gain-up involute devices.

If you’re neoteric to command-jabber upward job, prelude by recovering in the practice of surveying echo $? once commands don’t behave as intended. Rapidly, you’ll be gaining gain serviceability of of run away codes in your own scripts and valuing this simplistic but effective document system.

Related Posts