2011-03-24 75 views
3

我有點困惑,昨天我有這個工作,但它只是停止接受重定向標準輸入,幾乎神奇。重定向應用程序(java)的輸入,但仍然允許stdin在BASH中

set -m 
mkfifo inputfifo 
mkfifo inputfifo_helper 
((while true; do cat inputfifo; done) > inputfifo_helper)& 
trap "rm -f inputfifo inputfifo_helper java.pid; kill $!" EXIT 

exec 3<&0 
(cat <&3 > inputfifo)& 

NOW=$(date +"%b-%d-%y-%T") 

if ! [ -d "logs" ]; then 
    mkdir logs 
fi 

if [ -f "server.log" ]; then 
    mv server.log logs/server-$NOW.log 
fi 
java <inputfifo_helper -jar $SERVER_FILE & echo $! > java.pid && fg 

這是運行良好,我可以附和事情inputfifo和應用得到了它,並把它的控制檯,以及我可以直接輸入。它甚至通過屏幕。絕對沒有代碼明智的改變,但重定向的標準輸入已停止工作。我嘗試將文件描述符更改爲9,甚至127,但都沒有修復它。

我忘了什麼嗎?是否有一個特定的原因導致它不再有效? (我使用這個來代替發送輸入到屏幕本身,因爲我開始分離屏幕,它拒絕接收輸入,除非它至少連接了一次,我不知道這是一個錯誤還是打算)

+1

小心解釋爲什麼需要_helper fifo? – Ingo 2011-03-25 00:14:13

+0

我真的不記得了,但我懷疑它對我的重定向有任何影響。這個過程或屏幕的行爲就像他們被直接鍵入(鍵入什麼也不做),雖然就像我說的,它以前工作得很好。 – Dragonshadow 2011-03-25 00:59:59

回答

0

它的預感..但可能有別的附加到fd 0?

在我的Linux我看到這個

$ ls -l /dev/fd/ 
total 0 
lrwx------ 1 nhed nhed 64 Mar 24 19:15 0 -> /dev/pts/2 
lrwx------ 1 nhed nhed 64 Mar 24 19:15 1 -> /dev/pts/2 
lrwx------ 1 nhed nhed 64 Mar 24 19:15 2 -> /dev/pts/2 
lr-x------ 1 nhed nhed 64 Mar 24 19:15 3 -> /proc/6338/fd 

但以後每LS的PROC#指向FD3是不同的 - 我不知道這是怎麼回事(也許它綁在我的提示命令)但3的fd拍攝,儘量FDS#5-9

(和腳本用於診斷的頂部添加ls -l /dev/fd/

+0

我有同樣的事情,但改變它使用7或8什麼都不做。 – Dragonshadow 2011-03-25 00:53:10

0

運行你定的代碼的一個縮短版印刷的I/O錯誤消息:

cat: stdin: Input/output error 

快速修復是將stderr重定向到此命令的/ dev/null。

在Mac OS X/FreeBSD上,您也可以嘗試使用「cat -u」來禁用輸出緩衝(從而避免cat輸出緩衝問題)。

rm -v inputfifo inputfifo_helper 
mkfifo inputfifo inputfifo_helper 

(
((while true; do cat inputfifo; done) > inputfifo_helper) & 
# use of "exec cat" terminates the cat process automatically after command completion 
#((while true; do exec cat inputfifo; done) > inputfifo_helper) & 
pid1=$! 
exec 3<&0 # save stdin to fd 3 
# following command prints: "cat: stdin: Input/output error" 
#(exec cat <&3 >inputfifo) & 
(exec cat <&3 >inputfifo 2>/dev/null) & 
pid2=$! 
# instead of: java <inputfifo_helper ... 
(exec cat <inputfifo_helper) & 
pid3=$! 
echo $pid1,$pid2,$pid3 
lsof -p $pid1,$pid2,$pid3 
echo hello world > inputfifo 
) 


# show pids of cat commands 
ps -U $(id -u) -axco pid,command | grep cat | nl # using ps on Mac OS X 
+0

我使用的是Ubuntu 10.04,即使使用快速修復它也無法解決我的問題。我甚至試圖重定向貓的stderr文件來查看他們是否真的在我的結尾拋出了任何錯誤,並且都沒有。 – Dragonshadow 2011-03-25 20:56:33

0

嘗試使用單個fifo並將其回顯到r/w文件描述符中。 使用ASCII NUL字符終止您的(行)輸入,以便 讀命令繼續讀取,直到NULL字節(或EOF)。

rm -v inputfifo 
mkfifo inputfifo 
(
exec 0>&- 
exec 3<>inputfifo # open fd 3 for reading and writing 
echo "hello world 1" >&3 
echo "hello world 2" >&3 
printf '%s\n\000' "hello world 3" >&3 
# replaces: java <inputfifo_helper ... 
cat < <(IFS="" read -r -d '' <&3 lines && printf '%s' "$lines") 
) 
1

如果你能保持你的java程序中背景,您可以嘗試從控制終端/dev/tty讀寫使用,而讀循環來inputfifo。

# ... 
java <inputfifo_helper -jar $SERVER_FILE & echo $! > java.pid 

while IFS="" read -e -r -d $'\n' -p 'input> ' line; do 
    printf '%s\n' "${line}" 
done </dev/tty >inputfifo 
相關問題