- Bash command line exit codes demystified
- More Linux resources
- Extracting the elusive exit code
- Exit status 0
- Exit status 1
- Exit status 2
- Exit status 126
- Exit status 127
- Exit status 128
- Exit status 130
- Exit status 255
- Wrapping up
- The exit status code ‘126’ #10
- Comments
- mintaras commented Jul 8, 2014
- brunocascio commented Aug 1, 2014
- arthurkirkosa commented Sep 22, 2014
- shenka commented Nov 6, 2014
- carlituxman commented Nov 7, 2014
- barryvdh commented Nov 7, 2014
- carlituxman commented Nov 7, 2014
- shenka commented Nov 7, 2014
- barryvdh commented Nov 7, 2014
- carlituxman commented Nov 7, 2014
- barryvdh commented Nov 7, 2014
- carlituxman commented Nov 7, 2014
- shenka commented Nov 7, 2014
- shenka commented Nov 7, 2014
- barryvdh commented Nov 7, 2014
- shenka commented Nov 7, 2014
- shenka commented Nov 7, 2014
- retrogradeMT commented Apr 20, 2015
- arthurkirkosa commented Apr 21, 2015
- sherlyfebrianti96 commented Jun 9, 2015
- barryvdh commented Jun 9, 2015
- sherlyfebrianti96 commented Jun 22, 2015
- shaneparsons commented Oct 9, 2015
- yyxx9988 commented Sep 7, 2016
- Olis-HL commented Mar 2, 2017
- dheerajsharma494 commented Mar 4, 2017
- vhejda commented Jun 22, 2017
- enzotrompeneers commented Nov 15, 2018
- Sabbir1993 commented Jan 23, 2019
- astinaam commented Jun 27, 2019
- missbefore commented Aug 29, 2019
- opqclick commented Sep 2, 2019
- csb7 commented Dec 31, 2019
- preshan commented Apr 5, 2020 •
- stale bot commented Jul 29, 2020
Bash command line exit codes demystified
More Linux resources
When you execute a command or run a script, you receive an exit code. An exit code is a system response that reports success, an error, or another condition that provides a clue about what caused an unexpected result from your command or script. Yet, you might never know about the code, because an exit code doesn’t reveal itself unless someone asks it to do so. Programmers use exit codes to help debug their code.
Note: You’ll often see exit code referred to as exit status or even as exit status codes. The terms are used interchangeably except in documentation. Hopefully my use of the two terms will be clear to you.
I’m not a programmer. It’s hard for me to admit that, but it’s true. I’ve studied BASIC, FORTRAN, and a few other languages both formally and informally, and I have to say that I am definitely not a programmer. Oh sure, I can script and program a little in PHP, Perl, Bash, and even PowerShell (yes, I’m also a Windows administrator), but I could never make a living at programming because I’m too slow at writing code and trial-and-error isn’t an efficient debugging strategy. It’s sad, really, but I’m competent enough at copying and adapting found code that I can accomplish my required tasks. And yet, I also use exit codes to figure out where my problems are and why things are going wrong.
Exit codes are useful to an extent, but they can also be vague. For example, an exit code of 1 is a generic bucket for miscellaneous errors and isn’t helpful at all. In this article, I explain the handful of reserved error codes, how they can occur, and how to use them to figure out what your problem is. A reserved error code is one that’s used by Bash and you shouldn’t create your own error codes that conflict with them.
Enough backstory. It’s time to look at examples of what generates error codes/statuses.
Extracting the elusive exit code
To display the exit code for the last command you ran on the command line, use the following command:
The displayed response contains no pomp or circumstance. It’s simply a number. You might also receive a shell error message from Bash further describing the error, but the exit code and the shell error together should help you discover what went wrong.
Exit status 0
An exit status of 0 is the best possible scenario, generally speaking. It tells you that your latest command or script executed successfully. Success is relative because the exit code only informs you that the script or command executed fine, but the exit code doesn’t tell you whether the information from it has any value. Examples will better illustrate what I’m describing.
For one example, list files in your home directory. I have nothing in my home directory other than hidden files and directories, so nothing to see here, but the exit code doesn’t care about anything but the success of the command’s execution:
The exit code of 0 means that the ls command ran without issue. Although, again, the information from exit code provides no real value to me.
Now, execute the ls command on the /etc directory and then display the exit code:
You can see that any successful execution results in an exit code of 0, including something that’s totally wrong, such as issuing the cat command on a binary executable file like the ls command:
Exit status 1
Using the above example but adding in the long listing and recursive options ( -lR ), you receive a new exit code of 1:
Although the command’s output looks as though everything went well, if you scroll up you will see several «Permission denied» errors in the listing. These errors result in an exit status of 1, which is described as «impermissible operations.» Although you might expect that a «Permission denied» error leads to an exit status of 1, you’d be wrong, as you will see in the next section.
Dividing by zero, however, gives you an exit status of 1. You also receive an error from the shell to let you know that the operation you’re performing is «impermissible:»
Without a shell error, an exit status of 1 isn’t very helpful, as you can see from the first example. In the second example, you know why you received the error because Bash tells you with a shell error message. In general, when you receive an exit status of 1, look for the impermissible operations (Permission denied messages) mixed with your successes (such as listing all the files under /etc , as in the first example in this section).
Exit status 2
As stated above, a shell warning of «Permission denied» results in an exit status of 2 rather than 1. To prove this to yourself, try listing files in /root :
Exit status 2 appears when there’s a permissions problem or a missing keyword in a command or script. A missing keyword example is forgetting to add a done in a script’s do loop. The best method for script debugging with this exit status is to issue your command in an interactive shell to see the errors you receive. This method generally reveals where the problem is.
Permissions problems are a little less difficult to decipher and debug than other types of problems. The only thing «Permission denied» means is that your command or script is attempting to violate a permission restriction.
Exit status 126
Exit status 126 is an interesting permissions error code. The easiest way to demonstrate when this code appears is to create a script file and forget to give that file execute permission. Here’s the result:
This permission problem is not one of access, but one of setting, as in mode. To get rid of this error and receive an exit status of 0 instead, issue chmod +x blah.sh .
Note: You will receive an exit status of 0 even if the executable file has no contents. As stated earlier, «success» is open to interpretation.
I receiving an exit status of 126. This code actually tells me what’s wrong, unlike the more vague codes.
Exit status 127
Exit status 127 tells you that one of two things has happened: Either the command doesn’t exist, or the command isn’t in your path ( $PATH ). This code also appears if you attempt to execute a command that is in your current working directory. For example, the script above that you gave execute permission is in your current directory, but you attempt to run the script without specifying where it is:
If this result occurs in a script, try adding the explicit path to the problem executable or script. Spelling also counts when specifying an executable or a script.
Exit status 128
Exit status 128 is the response received when an out-of-range exit code is used in programming. From my experience, exit status 128 is not possible to produce. I have tried multiple actions for an example, and I can’t make it happen. However, I can produce an exit status 128-adjacent code. If your exit code exceeds 256, the exit status returned is your exit code subtracted by 256. This result sounds odd and can actually create an incorrect exit status. Check the examples to see for yourself.
Using an exit code of 261, create an exit status of 5:
To produce an errant exit status of 0:
If you use 257 as the exit code, your exit status is 1, and so on. If the exit code is a negative number, the resulting exit status is that number subtracted from 256. So, if your exit code is 20, then the exit status is 236.
Troubling, isn’t it? The solution, to me, is to avoid using exit codes that are reserved and out-of-range. The proper range is 0-255.
Exit status 130
If you’re running a program or script and press Ctrl-C to stop it, your exit status is 130. This status is easy to demonstrate. Issue ls -lR / and then immediately press Ctrl-C:
There’s not much else to say about this one. It’s not a very useful exit status, but here it is for your reference.
Exit status 255
This final reserved exit status is easy to produce but difficult to interpret. The documentation that I’ve found states that you receive exit status 255 if you use an exit code that’s out of the range 0-255.
I’ve found that this status can also be produced in other ways. Here’s one example:
Some independent authorities say that 255 is a general failure error code. I can neither confirm nor deny that statement. I only know that I can produce exit status 255 by running particular commands with no options.
Wrapping up
There you have it: An overview of the reserved exit status numbers, their meanings, and how to generate them. My personal advice is to always check permissions and paths for anything you run, especially in a script, instead of relying on exit status codes. My debug method is that when a command doesn’t work correctly in a script, I run the command individually in an interactive shell. This method works much better than trying fancy tactics with breaks and exits. I go this route because (most of the time) my errors are permissions related, so I’ve been trained to start there.
Have fun checking your statuses, and now it’s time for me to exit.
[ Want to try out Red Hat Enterprise Linux? Download it now for free. ]
Источник
The exit status code ‘126’ #10
Comments
mintaras commented Jul 8, 2014
I have some strange exception related with wkhtmltopdf driver premissions.
The exit status code ‘126’ says something went wrong: stderr: «sh: 1: /vendor/project_name/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64: Permission denied » stdout: «» command: /opt/project_name/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 —lowquality ‘/tmp/knp_snappy53bc0f6b50a847.44217361.html’ ‘/opt/project_name/app/storage/tmp/bill-123.pdf’.
Example code I’m using:
$snappy = \App::make(‘snappy.pdf’);
$snappy->generateFromHtml(‘
You owe me money, dude.
Thank you for any guidance.
The text was updated successfully, but these errors were encountered:
brunocascio commented Aug 1, 2014
vendor/project_name/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64: Permission denied
Does the user running the server file permissions?
arthurkirkosa commented Sep 22, 2014
I’m having the same issue on a mac. Has anyone found a fix for this?
shenka commented Nov 6, 2014
I’m having the same issue with this. I tried to change permissions for /home/vagrant/dev/dsm/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64
with no luck.
carlituxman commented Nov 7, 2014
I have similar issue on osx:
The exit status code ‘126’ says something went wrong: stderr: «sh: /URLTOMYPROJECT/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64: cannot execute binary file «
barryvdh commented Nov 7, 2014
Can you all run wkhtmltopdf standalone via the commandline?
carlituxman commented Nov 7, 2014
the same issue, in the vendor directory:
sh ./wkhtmltopdf-amd64
./wkhtmltopdf-amd64: ./wkhtmltopdf-amd64: cannot execute binary file
shenka commented Nov 7, 2014
Sorry barryvdh, I didn’t mentioned i’m with a vagrant vm.
/dev/dsm/vendor/h4cc/wkhtmltopdf-amd64/bin$ /home/vagrant/dev/dsm/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 —lowquality —orientation ‘landscape’ ‘/tmp/knp_snappy545c8064071457.95069716.html’ ‘/tmp/knp_snappy545c8064071ad1.89315423.pdf’
-bash: /home/vagrant/dev/dsm/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64: Permission denied
barryvdh commented Nov 7, 2014
So probably permission errors (make it executable) or the wrong binary. You could try downloading from http://wkhtmltopdf.org/downloads.html or using apt-get/yum
carlituxman commented Nov 7, 2014
I’ve install the package and try from /usr/local/bin. and copy the binaries to vendor/h4cc. and it works!
barryvdh commented Nov 7, 2014
You don’t have to copy the binaries, you can just change the path 😉
carlituxman commented Nov 7, 2014
ok, but I’ve tried two options to discard the problem
shenka commented Nov 7, 2014
I think my problem has to do with the permissions on shared folders in vagrant.
I tried to use chmod but since it’s a shared folder in vagrant, chmod doesn’t works.
Also, I tried on my Vagranfile:
settings[«folders»].each do |folder|
config.vm.synced_folder folder[«map»], folder[«to»], mount_options: [«dmode=777», «fmode=666»], type: folder[«type»] ||= nil
end
One thing I can’t explain is that this same vm works perfectly on a windows machine but it doesn’t work on my mac osx.
Please note that If I ‘ls -l’:
/dev/dsm/vendor/h4cc/wkhtmltopdf-amd64/bin$ ls -l
total 37916
-rw-rw-rw- 1 vagrant vagrant 38824584 Nov 6 22:42 wkhtmltopdf-amd64
Thanks in advance.
shenka commented Nov 7, 2014
Barry, I can not run wkhtmltopdf standalone via the command line
./wkhtmltopdf-amd64 http://www.google.com test.pdf
-bash: ./wkhtmltopdf-amd64: Permission denied
barryvdh commented Nov 7, 2014
Fix your permissions, make it executable.
shenka commented Nov 7, 2014
Barry, I can’t, The problem is that ngix runs with user ‘vagrant’ and group ‘vagrant’ but because it’s a sync_folder on my host side (not the vm) the permissions are different.
shenka commented Nov 7, 2014
Ok, so as I was typing my last response I figured it out what to do.
- In the vm the user who runs nginx and php it’s ‘vagrant’.
- Since ‘wkhtmltopdf-amd64’ is in a synced_folder, i had no way to set permissions (i think i tried everything).
- Copy wkhtmltopdf-amd64 to ‘/usr/local/bin/’ (in the vm of course).
- sudo chmod +x wkhtmltopdf-amd64
I don’t know if this is the perfect solution but it works.
Just let me know if it’s a better approach.
Thank you for your time.
retrogradeMT commented Apr 20, 2015
I’ve downloaded the package and got the same «126» error as others in this thread. I’ve followed Barry’s advice and separately downloaded the mac specific version to hard disk and changed the config reference from base_path(‘vendor. ‘) to /usr/local/bin/wkhtmltopdf’ and it works perfectly.
My problem is now is that I’m not sure how that translates to a live server?
arthurkirkosa commented Apr 21, 2015
I’ve downloaded the correct binaries for the wkhtmltopdf site and have different configurations for each env
sherlyfebrianti96 commented Jun 9, 2015
barryvdh, i tried to download the wkhtmltopdf
then i run via command line : sh wkhtmltopdf_0.9.9-3_i386.deb
but I got this : wkhtmltopdf_0.9.9-3_i386.deb: 2: wkhtmltopdf_0.9.9-3_i386.deb: Syntax error: newline unexpected
barryvdh commented Jun 9, 2015
Probably use your package manager or do something like: sudo dpkg -i wkhtmltopdf_0.9.9-3_i386.deb
sherlyfebrianti96 commented Jun 22, 2015
shaneparsons commented Oct 9, 2015
@shenka how did you get wkhtmltopdf to work from homestead to begin with?
Did you install it directly into homestead?
yyxx9988 commented Sep 7, 2016
to fix Permission denied problem.
Olis-HL commented Mar 2, 2017
Hi guys.
Please help me out in this issue.
The exit status code ‘2’ says something went wrong:
stderr: «/home/eps/public_html/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64: 2: /home/eps/public_html/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64: Syntax error: Unterminated quoted string
»
stdout: «»
command: /home/eps/public_html/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 —lowquality —no-background ‘/tmp/knp_snappy58b807df5b6579.76174024.html’ ‘po/HL/PDF/16-17/ORDER-PO_78787878.pdf’.
dheerajsharma494 commented Mar 4, 2017
Dude check if you machine is 32 bit or 64 bit.
32 bit systems:
$ composer require h4cc/wkhtmltopdf-i386 0.12.x
$ composer require h4cc/wkhtmltoimage-i386 0.12.x
or this if you are in 64 bit based system:
$ composer require h4cc/wkhtmltopdf-amd64 0.12.x
$ composer require h4cc/wkhtmltoimage-amd64 0.12.x
And then you can use it
vhejda commented Jun 22, 2017
I have had this problem too, even though the permissions were set correctly, the binary couldn’t be run. Turned out to be a ‘noexec’ flag set on the whole mounted partition where my /var/www is.
enzotrompeneers commented Nov 15, 2018
This worked for me (MAC):
- Install the package: https://wkhtmltopdf.org/downloads.html
- change in printer.php the binary path
‘pdf’ => array(
‘enabled’ => true,
// ‘binary’ => base_path(‘vendor/zendre4/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64’),
‘binary’ => ‘/usr/local/bin/wkhtmltopdf’,
‘timeout’ => false,
‘options’ => array(
‘disable-smart-shrinking’ => true,
‘page-size’ => ‘A4’,
‘print-media-type’ => true,
‘margin-bottom’ => 0,
‘margin-left’ => 0,
‘margin-right’ => 0,
‘margin-top’ => 0,
),
‘env’ => array(),
),
Sabbir1993 commented Jan 23, 2019
The exit status code ‘1’ says something went wrong: stderr: «The system cannot find the path specified. » stdout: «» command: /usr/local/bin/wkhtmltopdf —lowquality «C:\Users\SABBIR
astinaam commented Jun 27, 2019
Ok, so as I was typing my last response I figured it out what to do.
- In the vm the user who runs nginx and php it’s ‘vagrant’.
- Since ‘wkhtmltopdf-amd64’ is in a synced_folder, i had no way to set permissions (i think i tried everything).
- Copy wkhtmltopdf-amd64 to ‘/usr/local/bin/’ (in the vm of course).
- sudo chmod +x wkhtmltopdf-amd64
I don’t know if this is the perfect solution but it works.
Just let me know if it’s a better approach.
Thank you for your time.
Thanks. Solved the problem in my Fedora 30
missbefore commented Aug 29, 2019
«The exit status code ‘126’ says something went wrong:\nstderr: «sh: /usr/local/bin/wkhtmltopdf-amd64: cannot execute binary file\n»\nstdout: «»\ncommand: /usr/local/bin/wkhtmltopdf-amd64 —lowquality ‘http://www.github.com’ ‘/var/tmp/knp_snappy5d672fef28d7e9.77931025.pdf’.»
opqclick commented Sep 2, 2019
Same issue for me in Centos7(Digital Ocean droplet). Can anybody please help me with this?
csb7 commented Dec 31, 2019
if you use macOS ,
first of all go to http://wkhtmltopdf.org/downloads.html , then download the proper package according to your os . after installing the package do the followings :
in the config/snappy.php file , write the name of the
preshan commented Apr 5, 2020 •
@barryvdh Not working on Mac OS Catalina(10.15), Tried setting permission,
/usr/local/bin/wkhtmltopdf-amd64: cannot execute binary file
Also, make as executable
sudo chmod +x /usr/local/bin/wkhtmltopdf-amd64
sudo chmod +x /usr/local/bin/wkhtmltoimage-amd64
The exit status code ‘126’ says something went wrong: stderr: «sh: /usr/local/bin/wkhtmltopdf-amd64: cannot execute binary file » stdout: «» command: /usr/local/bin/wkhtmltopdf-amd64 —lowquality ‘/Applications/XAMPP/xamppfiles/htdocs/PM/storage/knp_snappy5e895a5340b1c2.79870225.html’ ‘/Applications/XAMPP/xamppfiles/htdocs/PM/storage/knp_snappy5e895a5340c375.37009111.pdf’.
Also, tried this on terminal «sudo sh /usr/local/bin/wkhtmltopdf-amd64 —lowquality https://stackoverflow.com/»
result:/usr/local/bin/wkhtmltopdf-amd64: cannot execute binary file
Am I using wrong binary? Im using «Cocoa | 64-bit | Installer (.pkg) for OS X 10.7 or later» this and downloaded from https://wkhtmltopdf.org/downloads.html.
My System Info:
Model Name: MacBook Pro
Model Identifier: MacBookPro14,1
Processor Name: Dual-Core Intel Core i5
Processor Speed: 2.3 GHz
Number of Processors: 1
Total Number of Cores: 2
L2 Cache (per Core): 256 KB
L3 Cache: 4 MB
Hyper-Threading Technology: Enabled
Memory: 8 GB
Boot ROM Version: 205.0.0.0.0
SMC Version (system): 2.43f9
stale bot commented Jul 29, 2020
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Any issues with PDF rendering itself that are not directly related to this package, should be reported on https://github.com/KnpLabs/snappy instead. When having doubts, please try to reproduce the issue with just snappy.
If you believe this is an actual issue with the latest version of laravel-snappy, please reply to this issue so we can investigate further.
Thank you for your contribution! Apologies for any delayed response on our side.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Источник