2011-03-21 108 views
0

如果我有一个命令行程序作为与命令行交互的后台线程(与./程序&一起执行)执行,我如何向它发送命令?有没有一种方法(比如使用'pipe')发送字符,就像它们在命令行中输入一样?将按键发送到后台应用程序

简化的命令行示例:

没有&(正常情况下):

~/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. 

一个PS -e |(我的情况!) grep testprogram显示它仍在运行,但现在不能在'kill'命令之外终止。

什么是理想的是类似如下:

究竟如何做到这一点?

回答

0

您可以将程序回用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,将预先做好的作业放回到后台。

相关问题