1

Im not sure if there is any way of doing this...

So, i run several EPICS programs in my machine. EPICS programs usually open interpreters that can be used as a CLI for the program. To make every terminal accessible, even for background processes, i sometimes use procServ to create a telnet server in such a way that you can remotely telnet into the server and gain access to the EPICS terminal. It should work similarly with python interpreters, for example.

Is there a way that, having started a process and forgotten to initialize it with procServ, i can gain access to the interpreter terminal?

I ask this because i have several processes that i sometimes need to access and, being processes started by other users in remote machines, sometimes the only way to gain access to the terminal is to kill the process and restart it in a terminal that i have access.

Expected behavior would be something like:

In terminal 1:

python3
x = 2

In terminal 2:

ps -aux | grep python3
my_user       <PID>  4.0  0.1  21272 11104 ?    S+   11:32   0:00 python3

Some_magic_command <PID> >>>#Im now in a python interpreter >>>print(x) 2

  • Can you show us the output of ps -elf | grep python3? Hopefully you will have the 13th column having things like pts/12 in it. With this you will be able to send input to the python3, but will not be able to directly get the output. However there are workarounds. – icarus Feb 02 '23 at 16:36
  • Yes, ps -elf | grep python3 gives me: 0 S my_user 21811 21768 0 80 0 - 5318 do_sel 14:05 pts/4 00:00:00 python3 and by doing sudo su -l then echo "x=2" > /dev/pts/4 "x=2" was printed on the python terminal, but it is not used by the interpreter: it is only printed but typing "x" and enter in the terminal gives me messages saying that x is not defined. – Marco Montevechi Filho Feb 02 '23 at 17:09
  • Also, doing cat /dev/pts/4 gives me some scrambled version of the things typed in the python terminal, but not all. It seems im missing something. – Marco Montevechi Filho Feb 02 '23 at 17:10
  • 2
    The cat /dev/pts/4 gives you the scrambled output because there are 2 things reading the output of the python3, the original terminal program and the cat. I will write a filler answer unless someone beats me to it. – icarus Feb 02 '23 at 17:18
  • 1
  • @muru thanks its a very helpful set of tools, but i couldnt find exactly what i want on it. Particularly, grab came with so many compilation errors that i gave up. reredirect came close but was only useful for redirecting output, not getting commands into terminal. – Marco Montevechi Filho Feb 07 '23 at 02:24

1 Answers1

1

There is not a perfect way to do this, but there is a tool reptyr (link) which does a reasonable job if the command is being run from a pty. It even has the desired user interface. I strongly suggest getting into the habit of using tmux or screen (I prefer tmux). Doing this reduces the need to background the process - see the warning on the github page.

icarus
  • 17,920
  • It comes close to what i need but it fails very frequently. I particularly made a bash file with: while true; do echo "testing"; done and executed it. Tried to ./reptyr <PID_of_TEST> and got: [-] Process 12057 (sleep) shares 12027's process group. Unable to attach. (This most commonly means that 12027 has sub-processes). Unable to attach to pid 12027: Invalid argument. Tried with test.sh subprocesses but to no avail.

    I ended up starting using tmux because of what you said but it doesnt solve my problem since i want this to access other users' process in the system i admin.

    – Marco Montevechi Filho Feb 07 '23 at 02:32
  • 1
    There isn't a perfect solution! I am puzzled by your example as the "while" loop doesn't have a "sleep" in it so I don't see where the sleep comes from. If your users use tmux then you can use su or sudo to become that user and use "tmux a" to attach to the running process. You can also use the -S option. I need to look up what EPICS is. – icarus Feb 07 '23 at 05:11
  • Sorry, thats my fault in copying the script to here: there was actually a sleep 1 as in: while true; do echo "testing"; sleep 1 done. Yes, i imagined there would be no perfect solution for it because what im asking itself is already a consequence of... not ideal process administration design. Anyway, thanks for the tips :). I dont think knowing what EPICS is is important for this question, but it is a development framework for industrial equipment control software. Its worth a look if you are curious. – Marco Montevechi Filho Feb 07 '23 at 12:54