How to fix "bad interpreter: No such file or directory Execution failed with exit code 126" Error while deploying a Shell script that calls another Shell script?

Follow

Shashank Srivastava -

Issue

You are deploying, via XLD, a Shell script (lets call it InstallTrigger.sh)that calls another Shell script (lets call it Install.sh) upon its deployment. When your first script calls the other one, you receive a "bad interpreter: No such file or directory Execution failed with exit code 126" error.

Environment

XL Deploy, Unix, Windows (scripts created on this platform)

Resolution

As explained in XebiaLabs KB Article, convert your target shell script(s) to a Unix/Unix like format by using a Linux utility dos2unix. It only works if you have your XLD running on a Linux server.

It will change the file encoding so that it is suited for execution on Linux platforms. Below is how to do it : -

root@shashank-server:/home/shashank/Downloads# file Install.sh
Install.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators
root@shashank-server:/home/shashank/Downloads# dos2unix Install.sh
dos2unix: converting file Install.sh to Unix format ...

If this utility is not installed, install it using your package manager.

If your XLD is running on Windows, you can use Notepad++ to convert CRLF to LF. Follow below screenshots to see how to do it.

Text_file_with_CRLF.png

Conversion_to_LF.png

Converted_to_LF.png

Cause

This happens most commonly when you create your shell scripts on a Windows platform using Notepad but your deployment platform is Linux. Files created on Windows platforms have a concept of CRLF (Carriage Return Line Feed) but Linux files have LF only. So, the shell scripts created on Windows might not be read properly by Linux.dos2unix/Notepad++ removes the CR part & makes files compatible with Unix/Unix like platforms.

Additional Information

Below log appears when your scripts have CRLF.

INFO c.x.d.p.cmd.step.ExecuteCommandStep - Executing command line : bash /home/shashank/Downloads/InstallTrigger.sh
2017-06-16 09:36:41.848 [task-sys-task.step-dispatcher-408] {stepDescription=Execute cmd, taskId=e2cde5be-147c-4870-95ef-be9f9ffae50a, username=admin} INFO c.x.overthere.ssh.SshConnection - Starting command [bash /home/shashank/Downloads/InstallTrigger.sh] on [ssh:sudo://xldeploy:root@192.168.0.50:22]
2017-06-16 09:36:41.902 [stderr reader] {} ERROR c.x.d.p.cmd.step.ExecuteCommandStep - /home/shashank/Downloads/InstallTrigger.sh: /home/shashank/Downloads/Install.sh: /bin/bash^M: bad interpreter: No such file or directory
2017-06-16 09:36:41.903 [task-sys-task.step-dispatcher-408] {stepDescription=Execute cmd, taskId=e2cde5be-147c-4870-95ef-be9f9ffae50a, username=admin} INFO c.xebialabs.overthere.ssh.SshProcess - Command [sudo -u root bash /home/shashank/Downloads/InstallTrigger.sh] on ssh:sudo://xldeploy:root@192.168.0.50:22 returned exit code 126
2017-06-16 09:36:41.904 [task-sys-task.step-dispatcher-408] {stepDescription=Execute cmd, taskId=e2cde5be-147c-4870-95ef-be9f9ffae50a, username=admin} ERROR c.x.d.p.cmd.step.ExecuteCommandStep - Command failed with return code 126

After conversion, deployment goes successful.

INFO c.x.d.p.cmd.step.ExecuteCommandStep - Executing command line : bash /home/shashank/Downloads/InstallTrigger.sh
2017-06-16 09:40:02.603 [task-sys-task.step-dispatcher-470] {stepDescription=Execute cmd, taskId=2ca67fa9-191f-4a7a-987a-4b8746da85b6, username=admin} INFO c.x.overthere.ssh.SshConnection - Starting command [bash /home/shashank/Downloads/InstallTrigger.sh] on [ssh:sudo://xldeploy:root@192.168.0.50:22]
2017-06-16 09:40:02.658 [stdout reader] {} INFO c.x.d.p.cmd.step.ExecuteCommandStep - Linux shashank-server 4.4.0-80-generic #101~14.04.1-Ubuntu SMP Fri Jun 9 16:31:42 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
2017-06-16 09:40:02.659 [stdout reader] {} INFO c.x.d.p.cmd.step.ExecuteCommandStep - 09:40:02 up 2:10, 3 users, load average: 0,44, 0,58, 0,60
2017-06-16 09:40:02.663 [task-sys-task.step-dispatcher-470] {stepDescription=Execute cmd, taskId=2ca67fa9-191f-4a7a-987a-4b8746da85b6, username=admin} INFO c.xebialabs.overthere.ssh.SshProcess - Command [sudo -u root bash /home/shashank/Downloads/InstallTrigger.sh] on ssh:sudo://xldeploy:root@192.168.0.50:22 returned exit code 0
Tags

xl-deploy, xld, encoding, scripts, exit code, how to, troubleshooting, kb

Have more questions? Submit a request
Powered by Zendesk