Python Forum
executing a bash file - revisited
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
executing a bash file - revisited
#1
Hi,

I'm trying to execute the line below from a .py file but I get an error: File not found.
However, if I execute it directly as sh restartMM.sh, it executes with no errors.

What am I doing wrong?

TIA

#sub.call("sh /home/pi/MagicMirror/restartMM.sh", shell=True) # same results p = sub.run("sh /home/pi/MagicMirror/restartMM.sh") print(p.returncode)
Reply
#2
First I assume that you have started your program with
import subprocess as sub
If not, the code makes no sense.

Even so, you try to invoke a shellscript, but that is not the intended use for subprocess. It is meant to start a process and one of the arguments may be a file. Your call makes subprocess try to interpret the argument as a file named "sh /home/pi/MagicMirror/restartMM.sh". If you take away "sh " it will complain about file permissions unless you have made it executable. Another way is:
import os p = os.system("sh /home/pi/MagicMirror/restartMM.sh") print(p)
Reply
#3
Try it like this :

from subprocess import Popen p = Popen ('/home/pi/MagicMirror/restartMM.sh', shell = True) code = p.wait () print (p.returncode)
Reply
#4
Thank you.

Actually, all the options in code shown below work within the MagicMirror directory but, they fail when executed from outside of it like in /home/pi.

-rwxr-xr-x 1 pi pi 58 Feb 9 18:32 restartMM.sh

ERROR:
FileNotFoundError: [Errno 2] No such file or directory: 'sh /home/pi/MagicMirror/restartMM.sh': 'sh /home/pi/MagicMirror/restartMM.sh'

#!/usr/bin/python3 import time import os import subprocess as sub os.system('sh /home/pi/MagicMirror/restartMM.sh') time.sleep(10) #sub.call("sh /home/pi/MagicMirror/restartMM.sh", shell=True) #time.sleep(10) #p = sub.run("sh /home/pi/MagicMirror/restartMM.sh") #print (p) #time.sleep(10) #p = sub.Popen ('/home/pi/MagicMirror/restartMM.sh', shell = True) #code = p.wait() #print (p.returncode)
Reply
#5
On MY Raspberry Pi 4B everything in the above code works correctly except :

p = sub.run("sh /home/pi/MagicMirror/restartMM.sh")
Sorry I can't help, but it seems that it's not python that's creating the issue.
Reply
#6
(Feb-10-2021, 04:38 PM)BashBedlam Wrote: On MY Raspberry Pi 4B everything in the above code works correctly except :

p = sub.run("sh /home/pi/MagicMirror/restartMM.sh")
Sorry I can't help, but it seems that it's not python that's creating the issue.
Ok, I agree. In fact, I did say the code works but, have executed it from a different folder?
Reply
#7
Quote:Ok, I agree. In fact, I did say the code works but, have executed it from a different folder?

I tested the code running from several different folders :
/home/
/home/pi/
/home/pi/Python/
/home/pi/MagicMirror/
From IDE, Vim and terminal. All worked the same.
Reply
#8
Try
import subprocess as sub sub.call(["sh", "/home/pi/MagicMirror/restartMM.sh"])
nilamo likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Function not executing each file in folder mathew_31 9 5,204 Aug-22-2022, 08:40 PM
Last Post: deanhystad
  Showing and saving the output of a python file run through bash Rim 3 7,420 Oct-06-2021, 10:48 AM
Last Post: gerpark
  file.write stops while true loop from executing in python3 boonr 3 4,682 Mar-25-2019, 12:50 PM
Last Post: ichabod801
  Executing external Python file in background and get output in python nakiscia 0 5,761 Feb-15-2018, 02:07 PM
Last Post: nakiscia
  executing a file saved in memory Skaperen 0 3,245 Sep-04-2017, 04:23 AM
Last Post: Skaperen

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.