I'm trying to execute a sh file from script python.
My script python
os.system('sh run.sh')
My sh file
echo 'The house is blue' | /opt/palavras/por.pl > output.txt
Error:
sh: 0: Cant' open run.sh
How can I fix it?
Make sure that your bash script has the right permissions (i.e it is executable). In a terminal run:
chmod +x run.sh
and then try (assuming that run.sh is in the same directory as your python script)
import os
os.system('./run.sh')
I do not believe this would run from the terminal either because you must run the file in order. Try:
os.system('sh chmod +x run.sh|./run.sh')
instead.
See: https://askubuntu.com/questions/38661/how-do-i-run-sh-files for details on running sh files and how to use os.system() in python for running an shell order for the use of | running in an order in a shell.
Related
I am using a launcher.sh for a raspberrypi 4B with Raspi os x64 to start a python script in a python env on startup. But setting the environment is problematic.
The file (test.sh) looks like:
. envname/bin/activate
echo Hi Stackoverflow
or alternatively
#!bin/bash
. envname/bin/activate
echo Hi Stackoverflow
using sh test.sh, correctly executes the shell script.
However, using it at launch with crontab
#reboot sh /home/pi/test.sh >/home/pi/logs/cronlog 2>&1
I get an error:
/home/pi/test.sh: 2: .: cannot open envname/bin/activate: No such file
How to properly set source #reboot to start the env on startup?
p.s. sh is not the solution.
With bin/sh and source envname/bin/activate it does not work as Ubuntu apparently uses bash rather than sh, and if sh is used the source command is not known. Bash uses . instead of source
Edit 1:
Normal Ubuntu uses bash for shell, but maybe that is different on startup? Could that be a way?
The answer from Goldilocks here tell us that the path has to be absolute, as crontab does not know where to find envname.
find | grep envname returned that it is at /home/pi/envname
so:
#!bin/bash
. /home/pi/envname/bin/activate
echo Hi Stackoverflow
does work.
I would like to submit Python scripts directly on a computing cluster using SGE. I have a test script:
#!/usr/bin/python
print "hello"
I have run chmod +x hello.py such that on the front end:
./hello.py
>> hello
However, when I run qsub -o out.log hello.py, the following files are generated:
out.log:
Warning: no access to tty (Bad file descriptor).
Thus no job control in this shell.
hello.py.e707589:
print: Command not found.
It seems to me that qsub is nonetheless trying to interpret the script as a bash script, even despite the header. How can I fix this?
This is the situation:
I've 2 script in python running in 2 different shells in Linux:
1° - python3 server.py
2° - python3 roomcontrol.py
I need that the user can restart roomcontrol.py from server.py.
I tried with subprocess:
from subprocess import call
dir = os.path.dirname(os.path.realpath(__file__)) + "/roomcontrol.py"
call(["python3",dir])
These instructions just start a new istance of "roomcontrol.py" in the shell of "server.py", I need to restart roomcontrol.py in his shell. Or close his shell and open a new one.
Edit:
I also tried:
import subprocess
dir = os.path.dirname(os.path.realpath(__file__)) + "/roomcontrol.py"
subprocess.Popen([dir], stdout=subprocess.PIPE, shell=True)
It doesn't work.
It writes a lot of stuff in the same shell of server.py and my cursor become a cross and if I click somewhere it wrtes stuff like before. A little example of what it writes:
import: unable to grab mouse `': Resource temporarily unavailable # error/xwindow.c/XSelectWindow/9199.
import: unable to grab mouse `': Resource temporarily unavailable # error/xwindow.c/XSelectWindow/9199.
.
.
.
from: can't read /var/mail/xml.dom
/home/stark/Desktop/TrackingOk/Release/roomcontrol.py: 9: /home/stark/Desktop/Tr: not foundlease/roomcontrol.py:
/home/stark/Desktop/TrackingOk/Release/roomcontrol.py: 10: /home/stark/Desktop/T: not foundelease/roomcontrol.py: try:
Create a new .sh file ("restart.sh" for example):
#!/bin/bash
kill $(pgrep -f 'python3 roomcontrol.py')
python3 roomcontrol.py &
Then just call
os.system('./restart.sh')
somewhere in your "server.py" script.
PS: You have to make the .sh file executable by running the following command:
chmod +x restart.sh
Edit: I'm not sure how you can start a process from a different shell, but you can start "roomcontrol.py" in another terminal window with the following (bash) command:
gnome-terminal -x sh -c 'python3 roomcontrol.py'
But then you'd have to replace "restart.sh" by
#!/bin/bash
kill -9 $(pgrep -f 'sh -c python3 roomcontrol.py')
gnome-terminal -x sh -c 'python3 roomcontrol.py'
I have a python script named script.py I want to double click on script.desktop, and it will execute the shell file script.sh, which will then evecute the python file: script.py.
My script.desktop file contains:
[Desktop Entry]
Name=jobs
Comment=jobs
Exec=/home/user/Desktop/school_scrape/script.sh
Icon=/home/user/Desktop/school_scrape/icon.png
Terminal=true
Type=Application
My shell file script.sh thus looks like:
#!/bin/bash
echo "hi"
sleep 2m
I eventually want it to execute a script.py file, upon executing it via the shell file, but itd make sense to first get it to echo hi first
I cant even get it to output the hi though in terminal when i double click script.desktop it just hangs with no error.
First I'd like for it to just run .desktop file, that runs the .sh file, and outputs hi, then ill worry about executing the python file.
Any idea what im doing wrong?
script.sh
#!/bin/bash
echo "hi"
read
Set chmod +x script.sh and try manually in console/terminal - you may have to add ./ at start to run it
./script.sh
script.desktop
In Linux Mint system adds first line with #! - so maybe it needs it.
#!/usr/bin/env xdg-open
[Desktop Entry]
Name=jobs
Comment=jobs
Exec=/home/user/Desktop/school_scrape/script.sh
Icon=/home/user/Desktop/school_scrape/icon.png
Terminal=true
Type=Application
Set chmod +x script.desktop and try click it
script.py
#!/usr/bin/env python
print("Hello World!")
input()
Set chmod +x script.py and try manually in console/terminal
./script.py
Add to script.sh - better with full path
#!/bin/bash
echo "hi"
/home/user/Desktop/school_scrape/script.py
(because script.py has shebang and set chmod +x so you don't have to use python in script.sh. You can even remove extension in file and in script)
Or use directly in .desktop
script-py.desktop
#!/usr/bin/env xdg-open
[Desktop Entry]
Name=jobs-py
Comment=jobs-py
Exec=/home/user/Desktop/school_scrape/script.py
Icon=/home/user/Desktop/school_scrape/icon.png
Terminal=true
Type=Application
Set chmod +x script-py.desktop and try click it
Have you tried to insert the following line in script.sh: python script.py
Or you can make it executable by using command: chmod +x script.py and just write in script.sh: ./script.py
I am using MacOSX Yosemite, I am trying to execute a python code without always typing the path or getting into the folder. I tried the following :
1) Added the line #! /usr/local/bin/python (after finding where the python is found)
2) sudo chmod a+x full_file_path
But this does not work for me. Nor
export PYTHONPATH=full_file_path
How else can I execute the python script without actually getting into the directory. I cannot also execute the script without using ./ the chmod does not change the access to executable. Which as far as I have seen many forums. It should.
You need to add full_file_path to your shell PATH variable. It is your shell that does the searching for the script, not Python. Only when the script has been found, is Python being started:
export PATH="full_file_path:$PATH"
You can add that line to your .bash_profile or .profile file in your home directory to make this addition permanent.
Run these commands without the $ signs in the front:
$ ls -l /full/directory/progname.py
$ chmod +x /full/directory/progname.py
$ ls -l /usr/local/bin/python
$ export PATH="$PATH:/full/directory"
$ progname.py
If any of the ls commands display an error message, then you are looking for the file in the wrong place, and you have to find the correct location, and update the command accordingly.
It's important to note that /usr/local/bin/python can also be wrong, for example some systems have the Python interpreter in /usr/bin/python.