Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Last active April 10, 2019 01:44
Show Gist options
  • Save mortymacs/c2d305dc73123855e9e1aa33a53fb647 to your computer and use it in GitHub Desktop.
Save mortymacs/c2d305dc73123855e9e1aa33a53fb647 to your computer and use it in GitHub Desktop.
Send and Read Message in a Python Process Via stdin in GNU/Linux

To read message from stdin try this sample code:

import time
import sys

while True:
    for i in sys.stdin:
        print(i)
    time.sleep(2)

Run it via python command:

$ python sample.py

To send message to this ran process try to find its PID:

$ pidof python sample.py
8184

Then send message to the stdin of process via below command:

$ echo "Hello World!" > /proc/8184/fd/0

Or to mix 2 last steps, try it:

$ echo "Hello World!" > /proc/$(pidof python sample.py)/fd/0

Reference: https://serverfault.com/questions/178457/can-i-send-some-text-to-the-stdin-of-an-active-process-running-in-a-screen-sessi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment