How to write data to existing process's STDIN from external process?
Your code will not work./proc/pid/fd/0
is a link to the /dev/pts/6
file.
$ echo 'foobar' > /dev/pts/6
$ echo 'foobar' > /proc/pid/fd/0
Since both the commands write to the terminal. This input goes to terminal and not to the process.
It will work if stdin intially is a pipe.
For example, test.py
is :
#!/usr/bin/python
import os, sys
if __name__ == "__main__":
print("Try commands below")
print("$ echo 'foobar' > /proc/{0}/fd/0".format(os.getpid()))
while True:
print("read :: [" + sys.stdin.readline() + "]")
pass
Run this as:
$ (while [ 1 ]; do sleep 1; done) | python test.py
Now from another terminal write something to /proc/pid/fd/0
and it will come to test.py
How do you stream data into the STDIN of a program from different local/remote processes in Python?
This isn't portable, but on many Linux systems, you can write to
/proc/$PID/fd/0
I think this may be one of a very limited number of potentially complicated options if you don't have any other control over the remote process.
Writing end-of-transmission to stdin of a running process
/proc/<PID>/fd/0
is a symlink to the terminal the process's stdin is connected to. For example, ls -l
might show /proc/<PID>/fd/0 -> /dev/pts/9
.
Trying to write to that link does not mean you'll write to the process's stdin; it means you'll write to whatever terminal that process is reading from. Your echo command is equivalent to echo "\r" > /dev/pts/9
.
(By the way, you need to write either echo -e "\r"
or echo $'\r'
to write a carriage return. Also on UNIX end-of-line is delimited by \n
not \r
. You can actually just write echo
to output a newline since it appends one automatically.)
I'd recommend finding a different way to signal your process to exit. The easiest method is to have it listen for SIGTERM
(sent by kill <PID>
) or SIGINT
(sent by Ctrl-C) and shut itself down when signalled.
C/C++ - Run system(process &) and then write to its stdin
I used the mplayer slave option and the input as a fifo file. It is working correctly.
Create the Linux fifo file with mkfifo:
system("mkfifo /tmp/slpiplay_fifo");
Open mplayer with:
system("mplayer -slave -idle -really-quiet -input file=/tmp/slpiplay_fifo /mnt/usb_slpiplay/* &");
Pass a "next" command to mplayer by using the fifo:
system("echo \"pt_step 1\" >> /tmp/slpiplay_fifo");
Starting and Controlling an External Process via STDIN/STDOUT with Python
process.communicate(input='\n') is wrong. If you will notice from the Python docs, it writes your string to the stdin of the child, then reads all output from the child until the child exits. From doc.python.org:
Popen.communicate(input=None) Interact
with process: Send data to stdin. Read
data from stdout and stderr, until
end-of-file is reached. Wait for
process to terminate. The optional
input argument should be a string to
be sent to the child process, or None,
if no data should be sent to the
child.
Instead, you want to just write to the stdin of the child. Then read from it in your loop.
Something more like:
process=subprocess.Popen([PathToProcess],stdin=subprocess.PIPE,stdout=subprocess.PIPE);
for i in xrange(StepsToComplete):
print "Forcing step # %s"%i
process.stdin.write("\n")
result=process.stdout.readline()
This will do something more like what you want.
Linux: write to stdin of python interpreter process and have that process evaluate input as code
It exists and it's called vim-slime
The only requirement is that you run the Python interpreter inside tmux
or screen
, or even better: byobu
Installing the vim-slime plugin is easy if you're using vim-pathogen:
cd ~/.vim/bundle
git clone git://github.com/jpalardy/vim-slime.git
See the vim-slime page for configuration details, but if you're using tmux
, simply add the following to your .vimrc
and re-start Vim:
let g:slime_target = "tmux"
Trying it out
Type in some Python code inside Vim:
def fib():
a, b = 0, 1
while 1:
yield a
a, b = b, a + b
Then press Ctrl-c-Ctrl-c to tell vim-slime to send the contents of your current buffer to another window. The first time you run it, vim-slime will ask you which screen
/tmux
window to send it to, but after that, press the key-sequence and it will send it wherever you told it to the first time.
vim-slime is visual-mode aware, too! If you only want to send a few lines to Python, enter visual-line mode with V, highlight the lines you want, and press the same Ctrl-c-Ctrl-c key sequence to send just those line.
How to write to the stdin of another app?
Have a look at the MSDN reference documentation for the following (both to be found in the System.Diagnostics
namespace):
Process.StandardInput
ProcessStartInfo.RedirectStandardInput
.
There's an example showing how to start a child process and write directly to its standard input.
For your particular example, here's how you set things up:
app1 starts app2 as a child process using the
Process
class (see links above).app1 writes to app2's standard input by writing to the
.StandardInput
stream of theProcess
object associated with app2.app2 just reads lines from its standard input (e.g. via
Console.ReadLine()
).
Related Topics
How to Open a New Tab in Gnome Terminal from Command Line
Bash Function to Find Newest File Matching Pattern
Setting the Umask of the Apache User
Separately Redirecting and Recombining Stderr/Stdout Without Losing Ordering
How to Kill All Processes With a Given Partial Name
How to Use Grep to Show Just Filenames on Linux
Find Multiple Files and Rename Them in Linux
Maximum Number of Processes in Linux
How to Configure Apache 2 to Run Perl Cgi Scripts
Setting Up Ftp on Amazon Cloud Server
How to Find the Last Field Using 'Cut'
Filter Log File Entries Based on Date Range
Understanding User File Ownership in Docker: How to Avoid Changing Permissions of Linked Volumes
Bash Ignoring Error for a Particular Command