Is it possible to communicate with a sub subprocess with subprocess.Popen?
I'm trying to write a python script that packages our software. This script needs to build our product, and package it. Currently we have other scripts that do each piece individually which include csh, and perl scripts. One such script is run like:
sudo mod args
where mod is a perl script; so in python I would do
proc = Popen(['sudo', 'mod', '-p', '-c', 'noresource', '-u', 'dtt', '-Q'], stderr=PIPE, stdout=PIPE, stdin=PIPE)
The problem is that this mod script needs a few questions answered. For this I thought that the traditional
(stdout, stderr) = proc.communicate(input='y')
would work. I don't think it's working because the process that Popen is controlling is sudo, not the mod script that is asking the question. Is there any way to communicate with the mod script and still run it through sudo?
Asked by: Lydia153 | Posted: 01-10-2021
I would choose to go with Pexpect.
Answered by: Emma101 | Posted: 02-11-2021
import pexpect child = pexpect.spawn ('sudo mod -p -c noresource -u dtt -Q') child.expect ('First question:') child.sendline ('Y') child.expect ('Second question:') child.sendline ('Yup')
I think you should remove the
sudo in your
Popen call and require the user of your script to type
This additionally makes more explicit the need for elevated privileges in your script, instead of hiding it inside
The simplest thing to do would be the run the controlling script (the Python script) via
sudo. Are you able to do that, or is that not an option?
We need more information.
- Is sudo asking you for a password?
- What kind of interface does the mod script have for asking questions?
Because these kind of things are not handled as normal over the pipe.
A solution for both of these might be Pexpect, which is rather expert at handling funny scripts that ask for passwords, and various other input issues.Answered by: Patrick571 | Posted: 02-11-2021
Python subprocess.Popen communicate through a pipeline
I want to be able to use Popen.communicate and have the stdout logged to a file (in addition to being returned from communicate(). This does what I want - but is it really a good idea? cat_task = subprocess.Popen(["cat"], stdout=subprocess.PIPE, stdin=subprocess.PIPE) tee_task = subprocess.Popen(["tee", "-a", "/tmp/logcmd"], stdin=cat_task.stdout, stdout = subprocess....
python - gobject and subprocess.Popen to communicate in a GTK GUI
I am trying to use a gobject to allow communication between a Popen process and a GTK GUI. Inspired by this: https://pygabriel.wordpress.com/2009/07/27/redirecting-the-stdout-on-a-gtk-textview/#comment-156 I implemented something similar to this:
Python communicate and subprocess.popen hung on reading stdin, when stdin is default None
I have a python daemon process. This daemon process spawns a thread to cater to a call to my daemon everytime a command is invoked. From within the thread, I use Python subprocess popen to execute such shell commands like def __executeCommand(self, cmd): try: self.__assertEmptyCommand(cmd) logger.debug('Executing command : '+str(cmd)) ...
Python subprocess.Popen poll seems to hang but communicate works
child = subprocess.Popen(command, shell=True, env=environment, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=sys.stdin, preexec_fn=os.setsid ) child_interrupted = False while child.poll() is None: ...
python - subprocess.Popen communicate fails randomly with zero returncode and no error
I have a routine that frequently executes a shell command and parses the output from it. For this I am using the subprocess.Popen to execute the shell command. Tested with the shell commands ls , systemctl status xxxx.service etc. Platform - Linux 3.14.4-yocto-standard x86_64 status_call = subprocess.Popen(shlex.split(<shell_command>), ...
Python 2.6 on Windows: how to terminate subprocess.Popen with "shell=True" argument?
Is there a way to terminate a process started with the subprocess.Popen class with the "shell" argument set to "True"? In the working minimal example below (uses wxPython) you can open and terminate a Notepad process happily, however if you change the Popen "shell" argument to "True" then the Notepad process doesn't terminate. import wx import threading import subprocess class MainWindow(wx.Frame): d...
Python's subprocess.Popen returns the same stdout even though it shouldn't
I'm having a very strange issue with Python's subprocess.Popen. I'm using it to call several times an external exe and keep the output in a list. Every time you call this external exe, it will return a different string. However, if I call it several times using Popen, it will always return the SAME string. =:-O It looks like Popen is returning always the same value from...
python - Is there a way to poll a file handle returned from subprocess.Popen?
Say I write this: from subprocessing import Popen, STDOUT, PIPE p = Popen(["myproc"], stderr=STDOUT, stdout=PIPE) Now if I do line = p.stdout.readline() my program waits until the subprocess outputs the next line. Is there any magic I can do to p.stdout so that I could read the output if it's there, but just continue otherwise? I'm lo...
multithreading - Python Subprocess.Popen from a thread
I'm trying to launch an 'rsync' using subprocess module and Popen inside of a thread. After I call the rsync I need to read the output as well. I'm using the communicate method to read the output. The code runs fine when I do not use a thread. It appears that when I use a thread it hangs on the communicate call. Another thing I've noticed is that when I set shell=False I get nothing back from the communicate when running i...
python - Using subprocess.Popen for Process with Large Output
I have some Python code that executes an external app which works fine when the app has a small amount of output, but hangs when there is a lot. My code looks like: p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) errcode = p.wait() retval = p.stdout.read() errmess = p.stderr.read() if errcode: log.error('cmd failed <%s>: %s' % (errcode,errmess)) ...
linux - Python subprocess.Popen erroring with OSError: [Errno 12] Cannot allocate memory after period of time
Note: This question has been re-asked with a summary of all debugging attempts here. I have a Python script that is running as a background process executing every 60 seconds. Part of that is a call to
Python: using threads to call subprocess.Popen multiple times
I have a service that is running (Twisted jsonrpc server). When I make a call to "run_procs" the service will look at a bunch of objects and inspect their timestamp property to see if they should run. If they should, they get added to a thread_pool (list) and then every item in the thread_pool gets the start() method called. I have used this setup for several other applications where I wanted to run a function ...
linux - Python subprocess.Popen "OSError: [Errno 12] Cannot allocate memory"
Note: This question was originally asked here but the bounty time expired even though an acceptable answer was not actually found. I am re-asking this question including all details provided in the original question. A python script is running a set of class functi...
python - Run command pipes with subprocess.Popen
How can I run the following command using subprocess.Popen? mysqldump database_name table_name | bzip2 > filename I know os.system() can do the job but I dont want to wai...
python - Encoding of arguments to subprocess.Popen
I have a Python extension to the Nautilus file browser (AFAIK this runs exclusively on GNU/Linux/Unix/etc environments). I decided to split out an expensive computation and run it as a subprocess, pickle the result and send it back over a pipe. My question concerns the arguments to the script. Since the computation requires a path argument and a boolean argument I figured I could do this in two ways: send the args...