将按键发送到后台应用程序

发布于 2024-10-23 19:21:03 字数 895 浏览 4 评论 0原文

如果我有一个作为与命令行交互的后台线程(也称为 ./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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唯憾梦倾城 2024-10-30 19:21:03

您可以使用 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。

You can bring the program back using 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>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文