executing a bash file - revisited - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: executing a bash file - revisited (/thread-32440.html) |
executing a bash file - revisited - ebolisa - Feb-09-2021 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) RE: executing a bash file - revisited - Serafim - Feb-09-2021 First I assume that you have started your program with import subprocess as subIf 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) RE: executing a bash file - revisited - BashBedlam - Feb-09-2021 Try it like this : from subprocess import Popen p = Popen ('/home/pi/MagicMirror/restartMM.sh', shell = True) code = p.wait () print (p.returncode) RE: executing a bash file - revisited - ebolisa - Feb-10-2021 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) RE: executing a bash file - revisited - BashBedlam - Feb-10-2021 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. RE: executing a bash file - revisited - ebolisa - Feb-10-2021 (Feb-10-2021, 04:38 PM)BashBedlam Wrote: On MY Raspberry Pi 4B everything in the above code works correctly except :Ok, I agree. In fact, I did say the code works but, have executed it from a different folder? RE: executing a bash file - revisited - BashBedlam - Feb-10-2021 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. RE: executing a bash file - revisited - Gribouillis - Feb-10-2021 Try import subprocess as sub sub.call(["sh", "/home/pi/MagicMirror/restartMM.sh"]) |