2011-03-21 103 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,將預先做好的作業放回到後臺。

相關問題