Run python scripts with sudo motion can't get output - python

import subprocess
p = subprocess.Popen(['sudo motion'],
shell = True,
stdout = subprocess.PIPE,
stdin = subprocess.PIPE)
p.wait()
stdout, stderr = p.communicate()
out = stdout.decode('utf-8')
print('-----------', out)
and i use "python3 sh2.py" running the above script but get the following results
[22636168:motion] [NTC] [ALL] conf_load: Processing thread 0 - config file /etc/motion/motion.conf
[22636168:motion] [NTC] [ALL] motion_startup: Motion 4.1.1 Started
[22636168:motion] [NTC] [ALL] motion_startup: Logging to file (/var/log/motion/motion.log)
-----------
How can I get the results

If you want to capture stderr via p.communicate(), you need to set stderr to subprocess.PIPE in your Popen call (by contrast, it doesn’t seem like you’re using stdin):
p = subprocess.Popen(['sudo motion'],
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

By default, motion starts as a daemon, running in the background.
Depending on what are doing, you probably want configure it to run your script when some event occurs, using one of the on_ configuration settings (on_event_start, on_picture_save, etc).

Related

output is not going to stdout

I have some executable which is a command processor. I'm trying to capture its response to commands. I use python subprocesses.
Below is my script:
import subprocess
# Open the subprocess
proc = subprocess.Popen('comproc.exe', \
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr = subprocess.STDOUT)
# Write a command
proc.stdin.write('help cfg_open\n')
# Close the input stream so the subprocess knows to exit
proc.stdin.close()
data = 0
while data!="":
data = proc.stdout.readline()
print data
# Wait for subprocess to exit
exit_status = proc.wait()
The output ("Welcome to command proc (c) 2015 ...") before I issue the "help cfg_open" is captured, while the response to "help cfg_open" is not.
The stderr is captured correctly if I issue some non-existing command.
Redirecting via cmd is working excellent:
c:\>comproc.exe >1.txt
help cfg_open
exit
c:\>
I get the whole output in 1.txt.
Would be grateful for any help!

Communication of a python parent process and a python subprocess

I have only recently started working with the subprocess-module, so i am sure, this is a rookie-question:
I am trying to start a python-subprocess from a python 3.5.2. parent script and retrieve information from it:
import subprocess
process = subprocess.Popen(
'C:\\IDLEX (Python GUI).exe',
shell = True,
stdout = subprocess.PIPE,
)
while True:
lines = process.stdout.readlines()
for line in lines:
print (line)
What command do i have to give in the child-process to generate an output in the parent process?
I already tried print('something') and sys.stdout.write('something else') (coupled with sys.stdout.flush()) but nothing seems to work.
The subprocess is running and its output is already directed to the parent process. There is no output generated because of 'C:\\IDLEX (Python GUI).exe' does not flush anything to stdout.
Your script is working:
process = subprocess.Popen(
'echo Hello World', # or change to any other executable or script to test
shell = True,
stdout = subprocess.PIPE,
)
Output:
Hello World
You may try to run C:\\IDLEX (Python GUI).exe directly in the cmd to check.

python subprocess is working in interactive mode but in not script

In windows I have to execute a command like below:
process = subprocess.Popen([r'C:\Program Files (x86)\xxx\xxx.exe', '-n', '#iseasn2a7.sd.xxxx.com:3944#dc', '-d', r'D:\test\file.txt'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process.communicate()
This works fine in python interactive mode, but not at all executing from the python script.
What may be the issue ?
Popen.communicate itself does not print anything, but it returns the stdout, stderr output. Beside that because the code specified stdout=PIPE, stderr=... when it create Popen, it catch the outputs (does not let the sub-process print output directly to the stdout of the parent process)
You need to print the return value manually:
process = ....
output, error = process.communicate()
print output
If you don't want that, don't catch stdout output by omit stdout=PIPE, stderr=....
Then, you don't need to use communicate, but just wait:
process = subprocess.Popen([...], shell=True)
process.wait()
Or, you can use subprocess.call which both execute sub-process and wait its termination:
subprocess.call([...], shell=True)

Subprocess is not invoking my command(or is doing it wrong)

Overview:
I have an application that sometimes must make something with celery- and if it is simple task such as count something- everything is ok.
I' ve got one task which must convert existing file to another file using MS Windows program. So- I installed WINE, then installed application and added folowing task to my tasks.py:
def convert_file( fil, to_format = 'pdf', save_to = '/tmp', callback = None ):
devnull = open( '/dev/null', 'w' )
commands = "xvfb-run -a wine '[ABSOLUTE_PATH_TO_WINDOWS_APP]' /r /f 104 %s" % fil
p = subprocess.Popen( commands, shell=True, cwd='/home/test', env=os.environ, stdout = devnull, stderr = subprocess.STDOUT )
p.wait()
devnull.close()
if callback:
subtask( callback ).delay( )
else:
return outfile
Problem:
The command isn't called or is called but nothing is happening(there isn't new file anywhere in filesystem)- but if I'll call this command from bash or from interactive python shell, everything is ok.
Edit:
When I'm calling the command from command line I get this:
test#ubuntu:~$ xvfb-run -a /home/test/.wine/....exe /r /f 104 /home/test/fs/...
err:winediag:X11DRV_WineGL_InitOpenglInfo The Mesa OpenGL driver is using software rendering, most likely your OpenGL drivers haven't been installed correctly
test#ubuntu:~$ XIO: fatal IO error 11 (Zasoby chwilowo niedostępne) on X server ":99"
after 1262 requests (1226 known processed) with 0 events remaining.
[Here i must press enter]
test#ubuntu:~$
Use
p = subprocess.Popen( commands, shell=True, cwd='/home/test', env=os.environ, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
for your Popen command, then
print p.communicate()
p.wait()
print p.communicate()
To see what it prints to stdout and stderr and figure out what you're doing wrong.
Edit: Xvfb is a fake framebuffer; it doesn't have hardware acceleration. Try changing your wine settings not to require hardware acceleration / not to use OpenGL / to do software rendering with winecfg.

Failing to capture stdout from application

I have the following script:
import subprocess
arguments = ["d:\\simulator","2332.txt","2332.log", "-c"]
output=subprocess.Popen(arguments, stdout=subprocess.PIPE).communicate()[0]
print(output)
which gives me b'' as output.
I also tried this script:
import subprocess
arguments = ["d:\\simulator","2332.txt","atp2332.log", "-c"]
process = subprocess.Popen(arguments,stdout=subprocess.PIPE)
process.wait()
print(process.stdout.read())
print("ERROR:" + str(process.stderr))
which gives me the output: b'', ERROR:None
However when I run this at the cmd prompt I get a 5 lines of text.
d:\simulator atp2332.txt atp2332.log -c
I have added to simulator a message box which pops up when it launches. This is presented for all three cases. So I know that I sucessfully launch the simulator. However the python scripts are not caturing the stdout.
What am I doing wrong?
Barry.
If possible (not endless stream of data) you should use communicate() as noted on the page.
Try this:
import subprocess
arguments = ["d:\\simulator","2332.txt","atp2332.log", "-c"]
process = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sout, serr = process.communicate()
print(sout)
print(serr)
The following code gives me text output on stdout.
Perhaps you could try it, and then substitute your command for help
import subprocess
arguments = ["help","2332.txt","atp2332.log", "-c"]
process = subprocess.Popen(arguments,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.wait()
print 'Return code', process.returncode
print('stdout:', process.stdout.read())
print("stderr:" + process.stderr.read())

Categories

Resources