【问题标题】:Send keypresses to backgrounded application将按键发送到后台应用程序
【发布时间】:2011-07-19 10:04:30
【问题描述】:

如果我有一个命令行程序作为与命令行交互的后台线程(AKA 与 ./program &)执行,我如何向它发送命令?有没有办法(例如,使用“管道”)发送字符,就好像它们是在命令行上输入的一样?

简化的命令行示例:

没有&(正常情况):

~/home/temp> ./testprogram

~/home/temp> Welcome to Testprogram. Enter command:

~/home/temp> Welcome to Testprogram. Enter command: quit

~/home/temp> Goodbye!

使用 &(我的情况!):

~/home/temp> ./testprogram &

~/home/temp> Welcome to Testprogram. Enter command:

~/home/temp> quit

~/home/temp> Command not found.

A ps -e | grep testprogram 显示它仍在运行,但现在永远无法在“kill”命令之外终止。

理想的情况如下:

~/home/temp> ./testprogram &

~/home/temp> Welcome to Testprogram. Enter command:

~/home/temp> quit

~/home/temp> Command not found.

~/home/temp> quit | /cat/proc/testprogram

~/home/temp> Goodbye!

我该怎么做?

【问题讨论】:

    标签: linux command-line send


    【解决方案1】:

    您可以使用 fg 恢复程序:

    ~/home/temp> ./testprogram &
    Welcome to Testprogram. Enter command:
    ~/home/temp> ls /tmp  # do whatever else you want to do on the shell
    # ... later
    ~/home/temp> jobs     # what was running?
    [1]+  Running                 ./testprogram &
    ~/home/temp> fg       # Brings testprogram to foreground, does not re-prompt
    quit
    Goodbye!
    ~/home/temp>
    

    如果您有多个后台作业,您可以使用 "fg %1" "fg %2" 选择将哪个作业置于前台...

    注意:您可以通过停止 (Ctrl-z) 作业然后在 shell 提示符下键入 bg 将前台作业放回后台。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 2020-11-24
      • 1970-01-01
      相关资源
      最近更新 更多