简体   繁体   中英

Run a command from a script directly to another process

I'm currently working on a script which interact with another process. If it is relevant, the said process is a simdebug console. What I want is exiting it properly because when I kill the process itself, it creates a lock file .lck. The Simdebug console is waiting for inputs and closes on receiving quit, then q and n, both sperated by an enter keypress to validate the command.

I managed to send some commands to the Simdebug using

echo quit > /proc/< PID >/fd/1

But it only print the results of the echo and I can't find how to send a enter keypress, only new lines '\\n' .

I can't aswell manage to send a quit command which would execute directly in the Simdebug and not the terminal where it is sent from.

My question would be resolved if one of those two points is answered:

  • Is it possible to simulate a validate keypress as in : Term 1 : echo ifconfig ; echo < enter keypress> Which would then execute what's in the read buffer of the Term 2

  • Is there a way to already execute a commande in another process as in

Term 1 : < unknown syntax > pwd

Term 2 < shows pwd of term2 not term1>

Which would not be working only from terminal to terminal but with an already opened process in read mode.

This is actually a hard thing to do. If you send characters to the /proc/self/fd/0 device or similar stdin device link from a different master terminal then it will just output the characters to the output side of the master terminal of the other process.

With tools like expect or pdip or screen you can send anything you want to a process encapsulated in their pseudoterminals as if it comes from their master terminal. But if a process is running then it will already have it's own terminal.

You can be in luck if your console can be persuaded for a controlling terminal transfer with reptyr .

For example if your console has process id 999999 (and you have screen and reptyr installed and maybe did something to appease selinux or apparmor/yama protections):

screen -dmS automateconsole
screen -S automateconsole -p 0 -X stuff 'reptyr 999999^M'
screen -S automateconsole -p 0 -X stuff 'quit^M'
sleep 1s
screen -S automateconsole -p 0 -X stuff 'q^M'
sleep 1s
screen -S automateconsole -p 0 -X stuff 'n^M'
sleep 1s
screen -S automateconsole -p 0 -X stuff 'exit^M'

But note:

  • You probably should cleanup the program that init'ed the console.
  • On Ubuntu at least I could not reptyr processes from other SSH sessions.

https://github.com/nelhage/reptyr

http://theterminallife.com/sending-commands-into-a-screen-session/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM