Script execution exit code is 126, but permissions are good
I have PHP script which calls bash script using exec() function. Worked well until recent release. When I checked logs, I found that the script is now exiting with code 126.
Documentation says that exit code 126 is reserved and it means that command cannot be executed. Comment elaborates, that this is permission problem or command is not an executable. Besides that I googled the problem, and most of the comments over the Internet is that it’s about permissions. Said that, it’s not surprising that my eyes caught only first part of the comment about permissions.
We checked script permissions – they were totally ok. Then I found out that parent directories’ permissions also matter, checked them – also all good. Then checked permissions of a binary being called by our script – good too.
When we tried to execute the script from CLI, we’ve got the following error:
-bash: path/to/script.sh: /bin/bash^M: bad interpreter: No such file or directory
You see ^M? That’s our guy. ^M is also known as CR or \r – Windows line-ending. When it’s somewhere in the script it usually doesn’t cause problems. But it’s presence in shebang could not be excused, so we’ve got exit code 126. That’s that part of the documentation saying “command is not an executable”.
If you get exit code 126 and permissions look good, check line endings.