I'm trying to write GPS data into a CSV through Python 3 with RaspberryPi. Writing the file works when the commands are run directly through the console, but when it is in python, the file opens and then returns an error (usually that another process is running). We wrote in another line to kill the process, but it's still not writing to the CSV. Any tips?
import math
import time
import os
os.system('sudo fuser -k/dev/ttyAMAO')
os.system('stty -F /dev/ttyAMAO 9600')
os.system('sudo gpsd /dev/ttyAMAO -F /var/run/gpsd.sock')
os.system('sudo gpsmon /dev/ttyAMAO -l /home/pi/Desktop/GPSDATA.txt')
Please note that os.system() executes the command in a subshell. This means that the PID of the shell executing the command will changes at each command.
A simple solution is to chain the commands in you call to os.system.
Related
I have two files main.py& test.py
Suppose the main file main.py is running and after a point of time I want to run test.py
I cannot use:
import test or os.system("python test.py") because this run python file in same terminal but I want to run the test.py in other terminal
So I mean to say in one terminal main.py is running after a point a new terminal opens and run test.py
Any solutions?
Thanks :D
If I understand correctly you want to run a python script when some condition is fulfilled so I would recommend calling the "test.py" using a subprocess library (bear in mind there are other methods) like this:
import subprocess
if(your_condition):
subprocess.call(['python', 'test.py', testscript_arg1, testscript_val1,...])
as mentioned here: Using a Python subprocess call to invoke a Python script
I have issue while running subprocess in Python CGI script.
I am going to run python file as subprocess in python CGI script.
script.py
#!enable debugging
import cgitb
cgitb.enable()
print("Content-Type: text/html;charset=utf-8")
print()
import subprocess
p = subprocess.Popen(["sudo", "/usr/bin/python3", "test.py"], stdout=subprocess.PIPE)
test.py
f.open("test.txt", "a")
f.write("This is test")
f.close()
If I run script.py in console, it creates test.txt file successfully.
But If I run it on browser with Python CGI, it cannot create test.txt.
I thought, it can be caused by permission, so I tried to create test.txt in script.py directly, not on 'test.py', it is created successfully.
So, main issue is Python CGI script cannot run subprocess.
I cannot get any error while running on browser as Python CGI script.
How can I fix this issue?
Please if sudo does not work, look in the system log. There can be messages that help you with the debugging. The files are in /var/log and if you list by time ls -t you will see which ones changed just now.
First try without sudo. Make your file in a place where it does not need sudo permission like /tmp/test.txt. Then you know if the problem is sudo or something else.
I want to execute the following command via a python script:
sudo cat </dev/tcp/time.nist.gov/13
I can execute this command via the command line completely fine. However, when I execute it using subprocess, I get an error:
Command ['sudo','cat','</dev/tcp/time.nist.gov/13'] returned non-zero exit status 1
My code is as follows
import subprocess
subprocess.check_output(['sudo','cat','</dev/tcp/time.nist.gov/13'])
As I mentioned above, executing the command via the command line gives the desired output without any error. I am using the Raspbian Jessie OS. Can someone point me in the right direction?
You don't want to use subprocess for this at all.
What does this command really do? It uses a bash extension to open a network socket, feeds it through cat(1) to reroute it to standard output, and decides to run cat as root. You don't really need the bash extension, or /bin/cat, or root privileges to do any of this in Python; you're looking for the socket library.
Here's an all-Python equivalent:
#!/usr/bin/env python3
import socket
s = socket.create_connection(('time.nist.gov', 13))
try:
print(s.recv(4096))
finally:
s.close()
(Note that all of my experimentation suggests that this connection works but the daytime server responds by closing immediately. For instance, the simpler shell invocation nc time.nist.gov 13 also returns empty string.)
Give this a try:
import subprocess
com = "sudo cat </dev/tcp/time.nist.gov/13"
subprocess.Popen(com, stdout = subprocess.PIPE, shell = True)
I am familiar with how to open a terminal from Python (os.system("gnome-terminal -e 'bash -c \"exec bash\"'")), but is there a way to open another terminal running the same program that opened the new terminal?
For instance, if I was running a program called foo.py and it opened another terminal, the new terminal would also be running foo.py.
See this question, it's pretty close. You want to add sys.argv as a parameter, though:
import sys
import subprocess
cmd = 'xterm -hold -e ./{0}'.format(' '.join(sys.argv))
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Be sure you somehow check how many processes/terminals you run already, otherwise it will hang your machine in a matter of seconds.
I have a bit of code that I am designing to take a file, perform the dos2unix command on it, then copy that file to a file called INPUT, and then run a command that boots a program. From my code the first two tasks work flawlessly, however the script doesn't seem to excecute the command line which starts the program. However when I take the command line exactly as I have it written in the script, and pass it in terminal, it works fine.
here is the code:
import subprocess
import os
os.chdir('/home/mike/testing/crystal')
subprocess.Popen(['dos2unix mgo_input'], stdout=subprocess.PIPE, shell=True)
subprocess.call(['cp mgo_input INPUT'], shell=True)
subprocess.Popen(['mpirun -np 8 Pcrystal </dev/null &> mgo_singlepoint.out &'], stdout=subprocess.PIPE, shell=True)
it is the mpirun section of the code that seems to be getting hung up
Popen() returns before the called program finishes execution. Using call (or check_call, which checks return codes) can be a better solution. Better yet, use python for the conversion and the copy.
I'm not sure why you are piping stdout, but I'm going to assume you don't want dos2unix or mpirun to print to the screen, so I redirect them to /dev/null.
import subprocess
import shutil
import os
os.chdir('/home/mike/testing/crystal')
subprocess.check_call('dos2unix mgo_input', stdout=open('/dev/null','w'), shell=True)
shutil.copy2(mgo_input, 'INPUT')
subprocess.check_call('mpirun -np 8 Pcrystal', stdout=open('mgo_singlepoint.out', 'w'), stdstderr=subprocess.STDOUT, shell=True)