# Unable to execute shell script

**URL:** <https://forum.ansible.com/t/unable-to-execute-shell-script/38851>\
**Category:** Ansible Project\
**Created:** [August 18, 2024, 11:14am UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851 "2024-08-18T11:14:12Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yogita\_Patil](https://avatars.discourse-cdn.com/v4/letter/y/ed8c4c/32.png) [@Yogita\_Patil](https://forum.ansible.com/u/Yogita_Patil)\
**Post date:** [August 18, 2024, 11:14am UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/1 "2024-08-18T11:14:12Z")

</div>

Hi All,

I am simply unable to execute a shell script on the remote host - this is my code (all other tasks run correctly except the execute task)

- name: Power consumption test  
hosts: all  
tasks:

- name: Transfer the script  
ansible.builtin.copy:  
src: power\_consumption.sh  
dest: /usr/src/power\_consumption.sh  
mode: ‘0777’

- name: Execute the script

# command: ./usr/src/power\_consumption.sh

command: “{{ item }}”  
args:  
chdir: “/usr/src/”  
with\_items:

- “./power\_consumption.sh”  
ignore\_errors: true

- name: Remove shell script from remote server  
file:  
path: /usr/src/power\_consumption.sh  
state: absent

- name: Copy shell script output from remote server  
fetch:  
src: /usr/src/{{ ansible\_fqdn }}.txt  
dest: /usr/src  
mode: ‘0777’  
with\_fileglob:  
\_ “\*.txt”

I have also tried with the shell: , script: sh ways of executing the script and none of them work, can someone tell me what I am doing wrong? TIA.

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [August 18, 2024, 4:15pm UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/2 "2024-08-18T16:15:58Z")

</div>

Without any real data it’s impossible to tell.  
Can you at a minimum post the error message?

---

<div class="post-metadata">

**Author:** ![Alexsey\_S](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Alexsey\_S](https://forum.ansible.com/u/Alexsey_S)\
**Post date:** [August 19, 2024, 1:47pm UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/3 "2024-08-19T13:47:10Z")

</div>

Create a var for script i.e.: script\_path=“/some/path/script” and use that instead of hard coding path

---

<div class="post-metadata">

**Author:** ![Yogita\_Patil](https://avatars.discourse-cdn.com/v4/letter/y/ed8c4c/32.png) [@Yogita\_Patil](https://forum.ansible.com/u/Yogita_Patil)\
**Post date:** [August 21, 2024, 8:32am UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/4 "2024-08-21T08:32:49Z")

</div>

Thanks for the responses guys, but these are the errors I see -

The full traceback is:  
File “/tmp/ansible\_ansible.legacy.command\_payload\_uk3ys11b/ansible\_ansible.legacy.command\_payload.zip/ansible/module\_utils/basic.py”, line 2050, in run\_command  
cmd = subprocess.Popen(args, \*\*kwargs)  
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
File “/usr/lib/python3.11/subprocess.py”, line 1024, in **init**  
self.\_execute\_child(args, executable, preexec\_fn, close\_fds,  
File “/usr/lib/python3.11/subprocess.py”, line 1901, in \_execute\_child  
raise child\_exception\_type(errno\_num, err\_msg, err\_filename)  
failed: [100.91.187.42] (item=./power\_consumption.sh) =\> {  
“ansible\_loop\_var”: “item”,  
“changed”: false,  
“cmd”: “./power\_consumption.sh”,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “./power\_consumption.sh”,  
“\_uses\_shell”: false,  
“argv”: null,  
“chdir”: “/usr/src/”,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“stdin”: null,  
“stdin\_add\_newline”: true,  
“strip\_empty\_ends”: true  
}  
},  
“item”: “./power\_consumption.sh”,  
“msg”: “[Errno 8] Exec format error: b’./power\_consumption.sh’”,  
“rc”: 8,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “”,  
“stdout\_lines”:   
}

I also create a simple foo.sh and tried to execute it like this, but here also I get errors -  
foo.sh contents -  
#!/bin/sh  
echo “Hello world”

My TestShell script yml looks like this -

- name: Shell script test  
hosts: all  
tasks:

- name: Run a shell command and register its output as a variable  
ansible.builtin.shell: /usr/src/foo.sh  
register: foo\_result  
ignore\_errors: true

- name: Run a shell command using output of the previous task  
ansible.builtin.shell: /usr/src/bar.sh  
when: foo\_result.rc == 5

Output of the above yml gives this error -

fatal: [132.56.197.46]: FAILED! =\> {  
“changed”: true,  
“cmd”: “/usr/src/foo.sh”,  
“delta”: “0:00:00.002043”,  
“end”: “2024-08-21 08:32:06.317505”,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “/usr/src/foo.sh”,  
“\_uses\_shell”: true,  
“argv”: null,  
“chdir”: null,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“stdin”: null,  
“stdin\_add\_newline”: true,  
“strip\_empty\_ends”: true  
}  
},  
“msg”: “non-zero return code”,  
“rc”: 127,  
“start”: “2024-08-21 08:32:06.315462”,  
“stderr”: “/bin/sh: 1: /usr/src/foo.sh: not found”,  
“stderr\_lines”: [  
“/bin/sh: 1: /usr/src/foo.sh: not found”  
],  
“stdout”: “”,  
“stdout\_lines”:   
}

---

<div class="post-metadata">

**Author:** ![mahespth](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mahespth/32/252_2.png) [@mahespth](https://forum.ansible.com/u/mahespth)\
**Post date:** [August 21, 2024, 8:36am UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/5 "2024-08-21T08:36:22Z")

</div>

Hi,

This if often because your ’shell’ script has the incorrect shebang at the top. If its a bash script then add executable: /bin/bash and try again, or check your script.

Cheers.

---

<div class="post-metadata">

**Author:** ![Yogita\_Patil](https://avatars.discourse-cdn.com/v4/letter/y/ed8c4c/32.png) [@Yogita\_Patil](https://forum.ansible.com/u/Yogita_Patil)\
**Post date:** [August 21, 2024, 10:25am UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/6 "2024-08-21T10:25:09Z")

</div>

The bash was just a trial, the is the script that I really care about running which runs perfectly from the command line -  
myScript.yml

- name: Shell script test  
hosts: all  
tasks:

- name: Run a shell command  
ansible.builtin.shell: power\_consumption.sh  
register: foo\_result  
args:  
chdir: /usr/src  
ignore\_errors: true

- name: Run a script  
ansible.builtin.script:  
cmd: /usr/src/power\_consumption.sh  
ignore\_errors: true

- name: Run a script in another way  
ansible.builtin.script: /usr/src/power\_consumption.sh  
ignore\_errors: true

- name: Run a script via command line  
ansible.builtin.command: ./power\_consumption.sh  
args:  
chdir: /usr/src  
ignore\_errors: true

# - name: Run a script second way

# ansible.builtin.script:

# cmd: /usr/src/power\_consumption.sh

- name: Run a shell command using output of the previous task  
ansible.builtin.shell: /usr/src/bar.sh  
when: foo\_result.rc == 5

The contents of the power\_consumption.sh script are -

hostfqdn=$(hostname --fqdn)  
time=1  
declare T0=($(cat /sys/class/powercap/intel-rapl/_/energy\_uj)); sleep $time; declare T1=($(cat /sys/class/powercap/intel-rapl/_/energy\_uj))  
for i in “${!T0[@]}”; do echo - | awk “{printf "%.1f W", $((${T1[i]}-${T0[i]})) / $time / 1e6 }” \>\> $hostfqdn.txt ; done

I have no idea what I am doing wrong, I have tried to put the script in /usr/bin as well but same result for all tasks -

TASK [Run a script via command line] \*

The full traceback is:  
File “/tmp/ansible\_ansible.legacy.command\_payload\_p\_z2bc7h/ansible\_ansible.legacy.command\_payload.zip/ansible/module\_utils/basic.py”, line 2050, in run\_command  
cmd = subprocess.Popen(args, \*\*kwargs)  
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
File “/usr/lib/python3.11/subprocess.py”, line 1024, in **init**  
self.\_execute\_child(args, executable, preexec\_fn, close\_fds,  
File “/usr/lib/python3.11/subprocess.py”, line 1901, in \_execute\_child  
raise child\_exception\_type(errno\_num, err\_msg, err\_filename)  
fatal: [100.91.187.42]: FAILED! =\> {  
“changed”: false,  
“cmd”: “./power\_consumption.sh”,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “./power\_consumption.sh”,  
“\_uses\_shell”: false,  
“argv”: null,  
“chdir”: “/usr/src”,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“stdin”: null,  
“stdin\_add\_newline”: true,  
“strip\_empty\_ends”: true  
}  
},  
“msg”: “[Errno 2] No such file or directory: b’./power\_consumption.sh’”,  
“rc”: 2,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “”,  
“stdout\_lines”:   
}

TASK [Run a script in another way] and TASK [Run a script]

The full traceback is:  
NoneType: None  
fatal: [100.91.187.42]: FAILED! =\> {  
“changed”: true,  
“msg”: “non-zero return code”,  
“rc”: 2,  
“stderr”: “Shared connection to 100.91.187.42 closed.\r\n”,  
“stderr\_lines”: [  
“Shared connection to 100.91.187.42 closed.”  
],  
“stdout”: “/root/.ansible/tmp/ansible-tmp-1724235551.658032-3341659-128056030829200/power\_consumption.sh: 3: Syntax error: "(" unexpected\r\n”,  
“stdout\_lines”: [  
“/root/.ansible/tmp/ansible-tmp-1724235551.658032-3341659-128056030829200/power\_consumption.sh: 3: Syntax error: "(" unexpected”  
]  
}

TASK [Run a shell command

fatal: [100.91.187.42]: FAILED! =\> {  
“changed”: true,  
“cmd”: “power\_consumption.sh”,  
“delta”: “0:00:00.001867”,  
“end”: “2024-08-21 10:19:11.554061”,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “power\_consumption.sh”,  
“\_uses\_shell”: true,  
“argv”: null,  
“chdir”: “/usr/src”,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“stdin”: null,  
“stdin\_add\_newline”: true,  
“strip\_empty\_ends”: true  
}  
},  
“msg”: “non-zero return code”,  
“rc”: 127,  
“start”: “2024-08-21 10:19:11.552194”,  
“stderr”: “/bin/sh: 1: power\_consumption.sh: not found”,  
“stderr\_lines”: [  
“/bin/sh: 1: power\_consumption.sh: not found”  
],  
“stdout”: “”,  
“stdout\_lines”:   
}

---

<div class="post-metadata">

**Author:** ![mahespth](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mahespth/32/252_2.png) [@mahespth](https://forum.ansible.com/u/mahespth)\
**Post date:** [August 21, 2024, 12:06pm UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/7 "2024-08-21T12:06:58Z")

</div>

Hi,

Your bash script must in include ‘#!/bin/bash’ to allow for its execution using bash hence the suggestion to add the executable verb to prove this was the case.

Regards

---

<div class="post-metadata">

**Author:** ![Yogita\_Patil](https://avatars.discourse-cdn.com/v4/letter/y/ed8c4c/32.png) [@Yogita\_Patil](https://forum.ansible.com/u/Yogita_Patil)\
**Post date:** [August 21, 2024, 12:12pm UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/8 "2024-08-21T12:12:20Z")

</div>

Yes yes I understand what you mean, and it does work if I add #!/bin/bash’ at the beginning of my foo.sh

contents of foo.sh -  
#!/bin/bash  
echo “Hello world”

However my power consumption script fails. Any idea what I am doing wrong there?

Many thanks for your help!

-Yogita.

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [August 21, 2024, 12:41pm UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/9 "2024-08-21T12:41:26Z")

</div>

Yes, but your power consumption script ALSO needs to have the correct shebang.

---

<div class="post-metadata">

**Author:** ![Yogita\_Patil](https://avatars.discourse-cdn.com/v4/letter/y/ed8c4c/32.png) [@Yogita\_Patil](https://forum.ansible.com/u/Yogita_Patil)\
**Post date:** [August 21, 2024, 12:43pm UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/10 "2024-08-21T12:43:22Z")

</div>

I see, ok let me try that! , it runs via cli without it so that is why I hadn’t added it. I will try that, thanks guys!

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 21, 2024, 12:46pm UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/11 "2024-08-21T12:46:16Z")

</div>

Yogita,

Do us a favor please and change this:

> ```auto
> ansible.builtin.command: ./power_consumption.sh
> 
> ```

to this:

> ```auto
> ansible.builtin.command: stat ./power_consumption.sh
> 
> ```

and let us see the results. Thanks.

---

<div class="post-metadata">

**Author:** ![Yogita\_Patil](https://avatars.discourse-cdn.com/v4/letter/y/ed8c4c/32.png) [@Yogita\_Patil](https://forum.ansible.com/u/Yogita_Patil)\
**Post date:** [August 21, 2024, 12:57pm UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/12 "2024-08-21T12:57:13Z")

</div>

Ok thanks Todd.

Also now I am getting a different error, after I cleaned up the yml to keep only 1 task -

FAILED! =\> {  
“ansible\_facts”: {  
“discovered\_interpreter\_python”: “/usr/bin/python3”  
},  
“changed”: true,  
“cmd”: [  
“stat”,  
“./usr/src/power\_consumption.sh”  
],  
“delta”: “0:00:00.002128”,  
“end”: “2024-08-21 16:53:50.376199”,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “stat ./usr/src/power\_consumption.sh”,  
“\_uses\_shell”: false,  
“argv”: null,  
“chdir”: null,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“stdin”: null,  
“stdin\_add\_newline”: true,  
“strip\_empty\_ends”: true  
}  
},  
“msg”: “non-zero return code”,  
“rc”: 1,  
“start”: “2024-08-21 16:53:50.374071”,  
“stderr”: “stat: cannot statx ‘./usr/src/power\_consumption.sh’: No such file or directory”,  
“stderr\_lines”: [  
“stat: cannot statx ‘./usr/src/power\_consumption.sh’: No such file or directory”  
],  
“stdout”: “”,  
“stdout\_lines”:   
}

Content of my yml are -

- name: Shell script test  
hosts: all  
gather\_facts: false  
tasks:

- name: Run a script in another way  
ansible.builtin.command: stat ./usr/src/power\_consumption.sh  
ignore\_errors: true

File does exist in the folder /usr/src on the controller node and the ansible host on which I am trying to execute the script both -

 ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/c/c9247e7e367295ba1e2d848db7a71e4800b989b0.png)

Contents of yml are -

---

<div class="post-metadata">

**Author:** ![Yogita\_Patil](https://avatars.discourse-cdn.com/v4/letter/y/ed8c4c/32.png) [@Yogita\_Patil](https://forum.ansible.com/u/Yogita_Patil)\
**Post date:** [August 21, 2024, 1:03pm UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/13 "2024-08-21T13:03:01Z")

</div>

This worked for me …hooray!!! Many thanks everyone for all your inputs and support!!!

- name: Shell script test  
hosts: all  
gather\_facts: false  
tasks:

- name: Start application  
shell: “/usr/src/power\_consumption.sh”  
args:  
chdir: “/usr/src”  
executable: “/bin/bash”

> **(attachments)**
>
> ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/c/c9247e7e367295ba1e2d848db7a71e4800b989b0.png)

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 21, 2024, 1:12pm UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/14 "2024-08-21T13:12:44Z")

</div>

The “stat” failed because you have a “.” in front of “/usr/src/power\_consumption.sh”.

> **(attachments)**
>
> ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/c/c9247e7e367295ba1e2d848db7a71e4800b989b0.png)

---

<div class="post-metadata">

**Author:** ![Yogita\_Patil](https://avatars.discourse-cdn.com/v4/letter/y/ed8c4c/32.png) [@Yogita\_Patil](https://forum.ansible.com/u/Yogita_Patil)\
**Post date:** [August 21, 2024, 1:16pm UTC](https://forum.ansible.com/t/unable-to-execute-shell-script/38851/15 "2024-08-21T13:16:36Z")

</div>

Ah ok thanks for letting me know, will fix that.

> **(attachments)**
>
> ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/c/c9247e7e367295ba1e2d848db7a71e4800b989b0.png)
