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.
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'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.
I have a simple ansible playbook which will call a shell script on a remote server, the shell script will call another python script, which will do something, when I run the ansible playbook, the script is not working, but when I ssh to the server and run the same command manually, it worked. I've done some debugging, seems when calling the python script, if I delete all the import statements from the python script, it works from ansible, but I don't understand why it works when I ssh to the server and would like to have some suggestion on how to resolve this issue.
the python script:
#!/usr/bin/python
import socket
import argparse
import logging
import subprocess
import time
import imp
def main():
f = open('/afile', 'w')
f.write('a test line')
f.close()
if __name__ == '__main__':
main()
those imports are not using here, it will be used in my real script, here I just write a line into a file for debugging.
The ansible playbooks are just simply like:
---
- hosts: servers
tasks:
- name: trigger the script
shell: /start.sh
The start.sh then simply invoke the python script:
#!/bin/sh
/start.py
sorry, it's my bad, I didn't put all the scripts here, seems that there is another script which has things like
#!/bin/sh
/start & >> stdout.log
this caused the problem, I guess the first three modules imported have things related to standard io, so the solution is using nohup.
again, very sorry for the incomplete question.
I am trying to run a script which in turn should execute a basic python script.
This is the shell script:
#!usr/bin/bash
mv ~/Desktop/source/movable.py ~/Desktop/dest
cd ~/Desktop/dest
pwd
ls -lah
chmod +x movable.py
python movable.py
echo "Just ran a python file from a shell script"
This is the python script:
#!usr/bin/python
import os
print("movable transfered to dest")
os.system("pwd")
os.system("mv ~/Desktop/dest/movable.py ~/Desktop/source")
print("movable transfered to dest")
os.system("cd ~/Desktop/source")
os.system("pwd")
Q1. The shell script is not executing the python file. What am I doing wrong?
Q2. Do I need to write the first line #!usr/bin/python in the python script?
Thank you.
You are missing a '/' in the shebang line:
#!usr/bin/python
should be
#!/usr/bin/python
Another thing I noticed was, you are calling cd in os.system. Since that command would be executed in a sub shell, it won't change the current directory of the calling script process.
I am trying out the sys library in python. In command prompt I am using this.
>>>import sys
>>>sys.ps1 ='$'
#my own input 'print 'test print''
$print 'test print'
test print
That worked in CLI, however, when I tried to do it in a python file and run it in CLI ( python file.py did not return anything.
#!/usr/bin/python
import sys
sys.ps1= '$'
Am I missing anything in the file?
What are you trying to do? sys.ps1 will just set the prompt for the python interpreter, so as soon as it exits it's effect is gone again. When running a file, there is no command-line.
If you want to see this have an effect, try running this with python -i file.py - this will run the command, and then drop you into the interactive shell, which should have PS1 set to $ now