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
11
votes
1 answer

subprocess.Popen using relative paths

The docs for Popen mention that you can't specify your executable path relative to the 'change working directory' kwarg. If cwd is not None, the child’s current directory will be changed to cwd before it is executed. Note that this directory is…
wim
  • 338,267
  • 99
  • 616
  • 750
11
votes
4 answers

How to (can I) ask a PIPE how many bytes it has available for reading?

I've implemented a non-blocking reader in Python, and I need to make it more efficient. The background: I have massive amounts of output that I need to read from one subprocess (started with Popen()) and pass to another thread. Reading the output…
Matt
  • 775
  • 7
  • 24
11
votes
1 answer

Python Popen Cannot Find the File Specified

I have the following code pathToFile = "R:\T2 Output\12345--01--Some File 1--ABCD.mp4" process = subprocess.Popen(['ffprobe.exe', '-show_streams', '"'+pathToFile+'"'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) I get the…
cyram
  • 820
  • 8
  • 22
11
votes
3 answers

how get the output from process opend by popen in php?

file a.php: file b.php: question: I can't see the echo result on console; why and how to see it? I don't want to do it in file b.php like:echo stream_get_contents($h);
argb
  • 111
  • 1
  • 1
  • 3
11
votes
1 answer

Python Popen sending to process on stdin, receiving on stdout

I pass an executable on the command-line to my python script. I do some calculations and then I'd like to send the result of these calculations on STDIN to the executable. When it has finished I would like to get the executable's result back from…
11
votes
2 answers

Disable console output from subprocess.Popen in Python

I run Python 2.5 on Windows, and somewhere in the code I have subprocess.Popen("taskkill /PID " + str(p.pid)) to kill IE window by pid. The problem is that without setting up piping in Popen I still get output to console - SUCCESS: The process with…
Denis Masyukov
  • 2,966
  • 4
  • 25
  • 20
10
votes
1 answer

WindowsError: [Error 5] Access is denied when trying to kill a subprocess (python)

So I have a python script that runs a loop in which it calls a program A through subprocess.Popen waits for its output, then saves the output and then calls it again and so on. (This keeps happening for a number of runs I set as an input) The thing…
Galois
  • 329
  • 1
  • 3
  • 10
10
votes
1 answer

Reproducing deadlock while using Popen.wait()

From the docs using Popen.wait() may: deadlock when using stdout=PIPE and/or stderr=PIPE and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use communicate() to avoid …
rok
  • 9,403
  • 17
  • 70
  • 126
10
votes
3 answers

Why does popen() invoke a shell to execute a process?

I'm currently reading up on and experimenting with the different possibilities of running programs from within C code on Linux. My use cases cover all possible scenarios, from simply running and forgetting about a process, reading from or writing to…
domsson
  • 4,553
  • 2
  • 22
  • 40
10
votes
3 answers

Killing processes opened with popen()?

I'm opening a long-running process with popen(). For debugging, I'd like to terminate the process before it has completed. Calling pclose() just blocks until the child completes. How can I kill the process? I don't see any easy way to get the pid…
Frank Farmer
  • 38,246
  • 12
  • 71
  • 89
10
votes
4 answers

Sending data to a program via stdin and ostream. (C++)

I would like to send data from within my C++ program to an external pipeline, like so: FILE* file = popen("my_prog -opt | other_prog", "w"); std::ostream fileStream = some_function(file); fileStream << "some data"; I understand there is no simple,…
Drew
  • 12,578
  • 11
  • 58
  • 98
10
votes
2 answers

Python 'subprocess' CalledProcessError: Command '[...]' returned non-zero exit status 1

Executing the following script... import socket import sys from collections import OrderedDict from subprocess import check_output from threading import Thread [...] class IpCheck(Thread): …
Basssprosse
  • 334
  • 1
  • 3
  • 17
10
votes
4 answers

How to pass SIGINT to child process with Python subprocess.Popen() using shell = true

I am currently trying to write (Python 2.7.3) kind of a wrapper for GDB, which will allow me to dynamically switch from scripted input to interactive communication with GDB. So far I use self.process = subprocess.Popen(["gdb vuln"], stdin =…
Mike
  • 111
  • 1
  • 1
  • 6
10
votes
2 answers

mingw: function not found when compiled with -std=c++11

I was trying to compile the code below (from https://stackoverflow.com/a/478960/683218). The compile went OK, if I compile with $ g++ test.cpp but went wrong when the -std=c++11 switch is used: $ g++ -std=c++11 test.cpp test.cpp: In function…
thor
  • 21,418
  • 31
  • 87
  • 173
10
votes
2 answers

C: Linux command executed by popen() function not showing results

I have the code below that I refer the thread on here to use the popen function int main(int argc,char *argv[]){ FILE* file = popen("ntpdate", "r"); char buffer[100]; fscanf(file, "%100s", buffer); pclose(file); …
sven
  • 1,101
  • 7
  • 21
  • 44