Got a bit of an odd question, one of those ones that’s hard to google for because you get general answers that don’t apply to my specific scenario… is it possible to create a new pseudo-terminal device in a bash script, without actually needing to spawn a new shell session and connect to it? Basically something like issuing mknod /dev/pts/myterminal followed by somecommand > /dev/pts/myterminal </dev/pts/myterminal to attach a process to it.
The easy solution is to use screen or spawn a new shell, but this doesn’t work in my situation because the script is run by a parent process to launch a child program which it then monitors for cpu/memory usage. (using screen means I’d get usage reports on the screen process, not the child process the stats are desired for). This also has the restriction that the child invoking script can’t/won’t run any commands after the child process has been started (until it quits) because the child is invoked with exec so that it inherits the script’s PID.
This child can accept commands via stdin, and the way it is launched currently results in stdin/stdout of the child being bound to /dev/null. What I’m trying to do is be able to at least assign stdin to a terminal device so I can interact by bash scripts sending commands to /dev/pts/<myterminal>. I have tried using a file for stdin but that doesn’t work for ongoing interactions after the process has started.
I know /dev/pts/ptmx can be interacted with to do this, per its documentation, but I’m not sure how to go about doing that with bash scripting; any approaches that I can think of to “open” the device and get it to create a new slave pts aren’t something I see working… for example cat /dev/pts/ptmx & would trigger creation of a new node, but doesn’t clear the nodes when finished, since cat is still in the background, nor does it “unlock” the slave device as required in other examples.
Any ideas? If the explanation of what I’m looking for isn’t clear, let me know and I’ll do my best to clarify.
It looks like it might, provided it doesn’t assign a new PID to the command being executed on the pseudoterminal; otherwise this wouldn’t be an issue and I could have just used screen
I’ll investigate further and see whether what I want to do is possible, thanks for the pointer.
I’ve succeeded in creating the /dev/pts device but not having any luck writing to it.
Specifically, what I want to do is issue commands to what is attached to the virtual terminal, e.g. echo ‘ls’ > /dev/pts/<n> and have the PTY on /dev/pts/n run the ‘ls’ command.
As it currently stands, all that happens is ‘ls’ is printed to the stdout of /dev/pts/<n>.
On Fri 17 Jan 2014 12:26:01 PM CST, VintagePC wrote:
It looks like it might, provided it doesn’t assign a new PID to the
command being executed on the pseudoterminal; otherwise this wouldn’t be
an issue and I could have just used screen
I’ll investigate further and see whether what I want to do is possible,
thanks for the pointer.
Hi
What about socat?
–
Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
openSUSE 13.1 (Bottle) (x86_64) GNOME 3.10.2 Kernel 3.11.6-4-desktop
If you find this post helpful and are logged into the web interface,
please show your appreciation and click on the star below… Thanks!
Thanks, that could be useful, but I think at this point my requirements are too esoteric and specific that it would be easier for me to take an alternate route to what I wish to achieve. I was hoping a simple script alteration could meet my needs but it looks to be more involved than that.
I’ve since found that pipes (not redirects… D’OH) work as desired, e.g. tail -f somefile | <process> works as I can echo “DERP”>somefile to my heart’s content and have <process> derp away, but unfortunately this changes the PID of <process>, and having <process> assume the invoked PID is a requirement beyond my control.