[英]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.