简体   繁体   English

从脚本直接运行命令到另一个进程

[英]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. 如果相关,则所述过程是simdebug控制台。 What I want is exiting it properly because when I kill the process itself, it creates a lock file .lck. 我想要的是正确退出它,因为当我杀死进程本身时,它会创建一个锁定文件.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. Simdebug控制台正在等待输入,并在接收到quit之后关闭,然后依次按下q和n(分别由回车按键来验证命令)。

I managed to send some commands to the Simdebug using 我设法使用发送了一些命令到Simdebug

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' . 但是它只打印回显的结果,我找不到发送回车按键的方法,只有新行'\\ 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. 我不能设法发送一个退出命令,该命令将直接在Simdebug中执行,而不是在发送该命令的终端中执行。

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 ; 是否有可能模拟验证按键,如下所示:术语1:echo ifconfig; echo < enter keypress> Which would then execute what's in the read buffer of the Term 2 echo <enter keypress>然后执行术语2的读取缓冲区中的内容

  • Is there a way to already execute a commande in another process as in 有没有办法在另一个过程中已经执行命令

Term 1 : < unknown syntax > pwd 条款1:<未知语法> pwd

Term 2 < shows pwd of term2 not term1> 术语2 <显示term2的pwd而不是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. 如果将字符从另一个主终端发送到/ proc / self / fd / 0设备或类似的stdin设备链接,则它将字符输出到另一个进程的主终端的输出侧。

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. 有了这样的工具expectpdipscreen ,你可以,如果它来自其主终端发送你想封装在他们的虚拟终端的过程东西。 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 . 如果可以说服您的控制台使用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): 例如,如果您的控制台具有进程ID 999999(并且您已经安装了screenreptyr并且也许做了一些事情来安抚selinux或apparmor / yama保护):

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. 至少在Ubuntu上,我无法从其他SSH会话reptyr进程。

https://github.com/nelhage/reptyr https://github.com/nelhage/reptyr

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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