-1

This seemed like something pretty obvious, but after a lot of searching, was not able to find the right search terms to get the answer. Searches for things like "connect to python kernel" brought up only pages and pages of stuff about Jupyter and IPython.

Bash

I found this which has something close using a coprocess.

My ideal thing would be:

create_named_process proc1
create_named_process proc2

Then someohow send a command to proc1 and get the output and send a command to proc2 and get the output. Then do it over again with more commands.

Python

I did find Python subprocess and bash subshells. But not sure how to keep those going in the background and connect to them. For instance, I could do

process = subprocess.Popen(['your_background_command'])
stdoutdata, stderrdata = process.communicate()

But I think if I kill the process I started the subprocess in, then the subprocess will die. (A commenter said that using Shell=True will cause the process to stay alive, but not sure how I would then access it. They removed the comment).

Also, how can I pass more commands to the process? Nothing like that in the documentation.

Note this post contains edits.

Community
  • 1
  • 1
abalter
  • 9,663
  • 17
  • 90
  • 145
  • Drive-by downvoter. If you don't like it, say why. If you think it's a stupid question, maybe it is. Maybe I'm stupid. But I'd still like to know how to do the "thing" in the question. – abalter Feb 24 '18 at 21:21

1 Answers1

1

I do not understand your kernel in the searches, but it seems that you are missing some basics about bash stdio/stderr handling.

A process has (normaly) 3 standard io streams:

  • stdin (standard input)
  • stdout (standard output)
  • stderr (standard error)

You can redirect all these IO streams with pipes (|) or files (using <,>,<< or >>). For example:

ls -l | grep "r-x" | sed 's/^.*:...//' > 5_files.list

You can, instead of a normal file, use a fifo:

mkfifo fifootje
tail -f fifootje | grep --line-buffered "r-x"  
ls -l > fifootje

or, with your command idea:

#!/bin/bash
mkfifo fifootje
while read line ; do
    case "$line" in
    (a) ls /tmp ;;
    (b) ls /var ;;
    (*) echo 'only a or b please' ;;
    esac
done

and then echo a or b (one per line) into the fifo.

Does that help?

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
  • I would like to be able to start shells that don't actually exist in a command window and interact with them. Like start a persistent, virtual bash shell, send something to stdin, capture stdout and stderr, send another command to stdin, etc. etc. But I want to be able to do it programmatically, not using a screen session or something. – abalter Feb 25 '18 at 15:58
  • So have you _tried_ the fifo? like in `mkfifo a; tail -f a | bash & ` and then `echo ls >> a`? Isn't that what you are asking (first part)? – Ljm Dullaart Feb 25 '18 at 17:15
  • Hey, that really seems to work. Are these getting executed in a separate process? Also, can I reattach to that fifo even if I've close the shell that originated it? I'll need to study fifo. – abalter Feb 25 '18 at 22:19
  • Perhaps I can also describe a little better that I'm trying to make something like a Jupyter notebook that can execute on cell in a Python Kernel, another cell in an R kernel, and another in a bash shell. – abalter Feb 25 '18 at 22:19