Linux exit code 127

127 Return code from $?

Value 127 is returned by /bin/sh when the given command is not found within your PATH system variable and it is not a built-in shell command. In other words, the system doesn’t understand your command, because it doesn’t know where to find the binary you’re trying to call.

You can try using which [program] to see which binary the OS is using. If it comes up empty, next step is checking execution bit and PATH.

@cr125rider, which is not particularly accurate — it doesn’t know about aliases, shell functions, PATH lookup memoization, or other factors internal to shell state. Much better to use type , a shell builtin which knows about all of those things.

This also happened to me with a file that had Windows line feeds. Correcting the line endings to unix format solved the problem

@MatthewKremer: Actually, I get 126 ( Permission denied ), not 127 when I attempt to invoke a non-executable file (irrespective of its contents); similarly, an attempt to execute a directory also results in 126 ( is a directory ).

but it can also mean that the command is found,
but a library that is required by the command is NOT found.

example: $caat The error message will

A shell convention is that a successful executable should exit with the value 0. Anything else can be interpreted as a failure of some sort, on part of bash or the executable you that just ran. See also $PIPESTATUS and the EXIT STATUS section of the bash man page:

 For the shell’s purposes, a command which exits with a zero exit status has succeeded. An exit status of zero indicates success. A non-zero exit status indicates failure. When a command terminates on a fatal signal N, bash uses the value of 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 com- mand 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. Shell builtin commands return a status of 0 (true) if successful, and non-zero (false) if an error occurs while they execute. All builtins return an exit status of 2 to indicate incorrect usage. Bash itself returns the exit status of the last command executed, unless a syntax error occurs, in which case it exits with a non-zero value. See also the exit builtin command below. 

Источник

Читайте также:  Red hat linux logo

How to fix exit status 127?

I attempted to follow this guide to run a Node application as a service. However, it is failing to start, with exit code 127. Is there any way to fix this? This is the journal.

sudo journalctl --follow -u serviceName -- Logs begin at Tue 2017-08-08 16:27:10 GMT. -- Aug 08 17:06:57 raspberrypi systemd[1]: Started serviceName. Aug 08 17:06:57 raspberrypi app.js[7234]: [46B blob data] Aug 08 17:06:57 raspberrypi systemd[1]: serviceName.service: main process exited, code=exited, status=127/n/a Aug 08 17:06:57 raspberrypi systemd[1]: Unit serviceName.service entered failed state. Aug 08 17:06:57 raspberrypi systemd[1]: serviceName.service holdoff time over, scheduling restart. Aug 08 17:06:57 raspberrypi systemd[1]: Stopping serviceName. Aug 08 17:06:57 raspberrypi systemd[1]: Starting serviceName. Aug 08 17:06:57 raspberrypi systemd[1]: serviceName.service start request repeated too quickly, refusing to start. Aug 08 17:06:57 raspberrypi systemd[1]: Failed to start serviceName. Aug 08 17:06:57 raspberrypi systemd[1]: Unit serviceName.service entered failed state. 
[Unit] Description=ServiceName After=network.target [Service] ExecStart=/home/pi/projects/ServiceName/app.js Restart=always User=root Group=root Environment=PATH=/usr/bin:/usr/local/bin Environment=NODE_ENV=production WorkingDirectory=/home/pi/projects/ServiceName [Install] WantedBy=multi-user.target 

1. node is /home/pi/.nvm/versions/node/v7.8.0/bin/node 2. Filesystem 1K-blocks Used Available Use% Mounted on /dev/root 6166268 4446224 1383764 77% /

Источник

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
Читайте также:  Linux bash for files

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.

Читайте также:  Установка компас 3d linux

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 .

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.

Источник

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