簡體   English   中英

為什么我不能使用Expect腳本啟動Redis?

[英]Why can not I use expect script to start redis?

我想可能有一個簡單的原因導致我無法像這樣開始Redis

----更新-----

在@larsks回答了我的問題之后,我意識到這是引起我困惑的原因:“您以一個交互語句結束它,這使您的控制台與所產生的進程的stdin / stdout一致。redis-server程序不是交互式的:它不接受任何控制台輸入。”

我再次檢查該代碼,發現正是該代碼使我認為該過程卡住了

#!/usr/bin/expect -f
spawn redis-server
expect "The server is now ready to accept connections"
interact
spawn redis-cli
expect ">"
...

我從未見過redis-cli運行。

但是如果我改變為

#!/usr/bin/expect -f
spawn redis-server
expect "The server is now ready to accept connections"
spawn redis-cli
expect ">"
...
interact //put it in the end.

它按我的預期工作。

順便說一句,我在這里使用期望的原因是首先確保Redis服務器啟動,然后刪除一些密鑰。

您期望第一個示例做什么? 您以一個interact語句結束它,該語句將您的控制台連接到您生成的進程的stdin / stdout。 redis-server程序不是交互式的:它不接受任何控制台輸入。 當您運行redis-server ,它將達到...

1135:M 18 Nov 13:59:51.634 * Ready to accept connections

...然后停止,等待redis客戶端連接並對其進行操作。 另外,請注意,我正在使用的Redis版本以Ready to accept connections結束Ready to accept connections而不是The server is now ready to accept connections ,因此在以下示例中將使用它。

我們可以在期望腳本中添加一個puts命令,以查看它實際上並沒有卡在任何地方。 如果我運行以下命令:

#!/usr/bin/expect -f
spawn redis-server
expect "Ready to accept connections"
puts "redis is running"
interact

我得到的輸出:

spawn redis-server
1282:C 18 Nov 14:03:33.123 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1282:C 18 Nov 14:03:33.123 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=1282, just started
[...]
1282:M 18 Nov 14:03:33.124 * Ready to accept connections
redis is running

因此,我們可以看到它並沒有卡在spawn語句,甚至沒有在expect語句。

什么是不是從你的問題清楚的是為什么你甚至使用expect在這種情況下,因為redis-server不是一個交互程序,並不會產生需要自動化的任何提示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM