Questions tagged [popen]

popen() is a way to communicate with subprocesses using a file-like interface. It originated in C, but has been ported to other languages (via extensions) such as Python.

FILE *popen(const char *command, const char *type)

popen() (Process OPEN) is a method by which programs can start and communicate with other programs using a file-like interface. This funcion is not mandated by ANSI, but is specified by POSIX.

popen() allows the programmer to avoid the internal workings of fork() and pipe() (this is how it is implemented on UNIX-like systems) by presenting a file object. This allows for the use of functions such as fprintf() and fscanf(), presenting a more orthogonal interface to the programmer (excepting closing the process - the programmer must use pclose() to stop a subprocess).

This function has been ported to a number of programming languages, such as Python (os.popen), Ruby (IO.popen), Tcl (open |command), etc.

2441 questions
19
votes
4 answers

Executing multiple commands using Popen.stdin

I'd like to execute multiple commands in a standalone application launched from a python script, using pipes. The only way I could reliably pass the commands to the stdin of the program was using Popen.communicate but it closes the program after the…
sz.
  • 191
  • 1
  • 1
  • 4
19
votes
2 answers

subprocess.Popen : how to pass a list as argument

I just need a hint on how to do things properly. Say I have a script called script.py which uses a list of names as argument ["name1", "name2", etc. ]. I want to call this script from another script using the subprocess module. So what I would like…
Serge
  • 1,018
  • 2
  • 11
  • 14
18
votes
10 answers

pipe large amount of data to stdin while using subprocess.Popen

I'm kind of struggling to understand what is the python way of solving this simple problem. My problem is quite simple. If you use the follwing code it will hang. This is well documented in the subprocess module doc. import subprocess proc =…
pietro abate
  • 459
  • 1
  • 4
  • 10
18
votes
2 answers

io.popen - how to wait for process to finish in Lua?

I have to use io.popen in Lua to run an executable which takes a command line argument. How to wait for a process to finish in the Lua so that expected output can be captured? local command = "C:\Program Files\XYZ.exe /all" hOutput =…
Chet
  • 193
  • 1
  • 1
  • 5
18
votes
5 answers

Ruby pipes: How do I tie the output of two subprocesses together?

Is there an automated way to do shell piping in Ruby? I'm trying to convert the following shell code to Ruby: a | b | c... > ... but the only solution I have found so far is to do the buffer management myself (simplified, untested, hope it gets my…
Arno
  • 181
  • 1
  • 1
  • 3
18
votes
3 answers

Python: executing shell script with arguments(variable), but argument is not read in shell script

I am trying to execute a shell script(not command) from python: main.py ------- from subprocess import Popen Process=Popen(['./childdir/execute.sh',str(var1),str(var2)],shell=True) execute.sh ---------- echo $1 //does not print anything echo $2…
creativeDrive
  • 245
  • 1
  • 5
  • 13
18
votes
1 answer

subprocess.call env var

I'm using Popen because I need the env, like this: Popen( ["boto-rsync", "..."], env={"PATH":"/Library/Frameworks/Python.framework/Versions/2.7/bin/"}, ) The problem is Popen runs the command as a new thread. Is there any way that I…
AliBZ
  • 4,039
  • 12
  • 45
  • 67
17
votes
5 answers

python subprocess: "write error: Broken pipe"

I have a problem piping a simple subprocess.Popen. Code: import subprocess cmd = 'cat file | sort -g -k3 | head -20 | cut -f2,3' % (pattern,file) p = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE) for line in p.stdout: …
mathtick
  • 6,487
  • 13
  • 56
  • 101
17
votes
4 answers

How to run a background process and do *not* wait?

My goal is simple: kick off rsync and DO NOT WAIT. Python 2.7.9 on Debian Sample code: rsync_cmd = "/usr/bin/rsync -a -e 'ssh -i /home/myuser/.ssh/id_rsa' {0}@{1}:'{2}' {3}".format(remote_user, remote_server, file1, file1) rsync_cmd2 =…
harperville
  • 6,921
  • 8
  • 28
  • 36
17
votes
1 answer

How to write EOF to STDIN popen in python

I have the following command run through popen: p = subprocess.popen(["/usr/bin/whiptail", "--title", "\"Progress\"", "--gauge", "\"\"", "6", "50", "0"], stdout=subprocess.PIPE, stding=subprocess.PIPE) To stop the whiptail command from running I…
gudge
  • 1,053
  • 4
  • 18
  • 33
16
votes
1 answer

Difference between subprocess.Popen preexec_fn and start_new_session in python

What is the difference between these two options to start a new process with subprocess.Popen for python3.2+ under Linux: proc = subprocess.Popen(args, ..., preexec_fn=os.setsid) # 1 proc = subprocess.Popen(args, ..., start_new_session=True) #…
Mikhail Geyer
  • 881
  • 2
  • 9
  • 27
16
votes
2 answers

Disable warnings while pip installing packages

Can I somehow disable warning from PIP while it installs packages? I haven't found such an option in pip usage! I'm trying to install packages using python script (2.7.8) and check whether it was successful: p = subprocess.Popen( 'pip install…
user2975881
  • 161
  • 1
  • 1
  • 5
16
votes
2 answers

What difference between subprocess.call() and subprocess.Popen() makes PIPE less secure for the former?

I've had a look at the documentation for both of them. This question is prompted by J.F.'s comment here: Retrieving the output of subprocess.call() The current Python documentation for subprocess.call() says the following about using PIPE for…
Nathan Basanese
  • 8,475
  • 10
  • 37
  • 66
16
votes
2 answers

Handling tcpdump output in python

Im trying to handle tcpdump output in python. What I need is to run tcpdump (which captures the packets and gives me information) and read the output and process it. The problem is that tcpdump keeps running forever and I need to read the packet…
ashish g
  • 429
  • 1
  • 7
  • 16
15
votes
3 answers

Python Subprocess Grep

I am trying to use the grep command in a python script using the subprocess module. Here's what I have: userid = 'foo12' p = subprocess.Popen(['grep', "%s *.log"%userid], stdout=subprocess.PIPE) And it returns nothing. I am not entirely sure what I…
Jason Zhu
  • 2,194
  • 3
  • 23
  • 27