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)
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)
Try it like this :
from subprocess import Popen
p = Popen ('/home/pi/MagicMirror/restartMM.sh', shell = True)
code = p.wait ()
print (p.returncode)
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)
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.
(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?
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.
Try
import subprocess as sub
sub.call(["sh", "/home/pi/MagicMirror/restartMM.sh"])