将按键发送到后台应用程序
如果我有一个作为与命令行交互的后台线程(也称为 ./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!
我到底该怎么做?
If I have a command line program executed as a backgrounded thread (AKA with ./program &) that interacts with the command line, how can I send commands to it? Is there a way (say, using 'pipe') to send characters as though they were being inputted on the command line?
Simplified command-line example:
Without & (normal case):
~/home/temp> ./testprogram
~/home/temp> Welcome to Testprogram. Enter command:
~/home/temp> Welcome to Testprogram. Enter command: quit
~/home/temp> Goodbye!
With & (my case!):
~/home/temp> ./testprogram &
~/home/temp> Welcome to Testprogram. Enter command:
~/home/temp> quit
~/home/temp> Command not found.
A ps -e | grep testprogram shows that it's still running, but now can never terminate outside of a 'kill' command.
What would be ideal is something like the following:
~/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!
How exactly do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 fg 将程序恢复:
如果您有多个后台作业,您可以使用“fg %1”“fg %2”选择将其置于前台...
注意:您可以将前台作业放回后台停止 (Ctrl-z) 作业,然后在 shell 提示符下键入 bg。
You can bring the program back using fg:
If you have more than one backgrounded jobs, you can choose which to foreground using "fg %1" "fg %2"...
Note: you can put a foregrounded job back into the background by stopping (Ctrl-z) the job then typing bg at the shell prompt.