I am trying to launch a .sh script from Python 3.3 in Ubuntu 13.10.
The script is supposed to shutdown the computer. I have already marked the sh script as executable through the terminal. I have tried to run the sh script through: os.system("script.sh"), subprocess.Popen("Script.sh"), and subprocess.call([script.sh]).
They keep returning the: OSError Exec format error.
Any help would be greatly appriciated!
I assume that script.sh isn't in your PATH but in your current working directory.
By default os.system and subprocess look in your path for the requested executable. So to execute something in your current working directory you need to specify the executable like this:
subprocess.call("./script.sh")
The ./simply says that the executable that should be executed is in the current working directory.
Related
Monit not invoking python script <--> OS is CentOS. first line in python script is "#!/usr/bin/env python3" when i tried to invoke python script from my terminal its working but monit is not able to trigger the script.
I tried to call python script from shell script in monit but no luck. even i tried adding PATH variable as second line to shell script.
any help will be appreciated.
Issue is with PATH environmental variable, like cron, monit is only taking subset of values from PATH variable instead of all values. So after explictly adding $PATH variable after shell interpreter, issue got resolved
In order to get some software running I need to 1. Run a script that will execute a remote license manage, 2. Execute a shell script to start the software. I can do this by opening a command window in the directory with the rlm , and then type ./rlm to run the Linux executable. Then I can go into the directory that contains the shell script, open a terminal in that location and run ./myshell.sh. This opens the GUI for my software.
I would like to run these steps using a single Python script. I have tried:
#change the working directory...
os.chdir(r'/opt/mysoftwarelocation')
#confirm location change...
print(os.getcwd() )
#run ./rlm...
os.system('./rlm')
At this point I can see from a python terminal that the rlm is running.
I would then like to run the code below to run the shell script...
os.chdir(r'/opt/mysoftwarelocation/sumsubdirectory')
print(os.getcwd() )
os.system('./some.sh')
Unfortunately, after os.system('./rlm') finishes the script stalls and will not execute further and without an errors.
How to I get the second part of my script to run within a single Python script?
Have you tried to run the rlm command in the background?
subprocess module gives a nice interface for that
I have two files :
run.sh
train.py
Content of both file
in run.sh
export TRAINING_DATA=test1
export FOLD=test2
python.exe train.py
in train.py
from os import environ
print(environ.get("TRAINING_DATA"), environ.get("FOLD"))
when I run the bash file through command line
bash run.sh
Instead of getting test1 & test2, I am getting None, None
Both the files are placed in the Scripts folder of my virtualenv. I am using Windows 10.
The issue you are hitting is that you are running the windows version of python.exe rather than the linux/bash version.
In the run.sh i see that you are refering to python.exe, this is the windows exe and as such when it runs it will not be in the same shell as the .sh script used to invoke it.
you can prove this to yourself by doing the following from within WSL
➜ which python.exe
/mnt/c/Python27/python.exe
➜ which python
/usr/bin/python
So as long as you have python installed in WSL you can simply swap out the .exe in the .sh script so that you are using the bash/linux version thus:
export TRAINING_DATA=test1
export FOLD=test2
python train.py
You should try disconnect and log back in on the session.
Happened to me and it's how I solved it.
On my side it was on a ssh console.
Have a great day/night-
My GUI script that is a PyQt5 file (.pyw extension) does work when running on my IDE with a build configuration that tells the compiler to run the script with python3:
And it also works when i tell to the regular terminal on Linux to run same script with python3 like this:
When runned with the default python (python2.7) on a regular terminal it tells: ImportError: No module named PyQt5.QtWidgets.
My code does it have these lines on the start to tell that is a python3 script like: #!/usr/bin/python3 or #!/usr/bin/env python3 (I have python3 installed).
When double clicked on the Linux Mint File Explorer the cursor turns crosshair and nothing happends, with the terminal option, same happends and a empty terminal shows. Im talking these options
I guess Linux Mint still runs the scripts with python2.7 even when I added the bash lines to tell
Someone knows why the lines:
#!/usr/bin/python3
#!/usr/bin/env python3
doesnt work when just double click?
I want to run the script from the Linux File Explorer without the need of an IDE or using the terminal.
Try chmod +x file.py and run it in terminal by using ./file.py also try lunching the file from a different path, like python3 ~/path/to/file.py and see if the error persists
I need subprocess in my script to connect with 3G. But unfurtunately it's throwing me alot of errors. So I was hoping maybe someone here could help me.
My code:
import subprocess
import time
subprocess.run('sudo ./sakis3g connect OTHER="USBMODEM" USBMODEM="12d1:1001" APN="internet"', shell=True)
When I run this simple script in my home directory I get the following error:
sudo: ./sakis3g: command not found
Is it maybe because the sakis3g script itself it located in /usr/local/bin. Any help would be appreciated, thanks in advance
I run this simple script in my home directory
./ means the current directory i.e. the home directory in this case.
Is it maybe because the sakis3g script itself it located in /usr/local/bin
/usr/local/bin is (likely) not your home directory. sudo can't find sakis3g in the current directory because there is no such file.
Use /usr/local/bin/sakis3g instead of ./sakis3g.
copy that saskis3g object file in your directory where you have your code then you can use the command directly without including any file path for the object file(i.e sakis3g) in your connect command.
if suppose you have a folder named sample. and your program resides in this folder then copy+paste the sakis3g object file to this folder[you can do this by using the CP command in linux ] and use the normal command ie " system("sudo ./sakis3g connect"); " no need of any path.