简体   繁体   English

为什么我不能使用Expect脚本启动Redis?

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

I guess there must be a simple reason why I can't start redis like this 我想可能有一个简单的原因导致我无法像这样开始Redis

---- update ----- ----更新-----

After @larsks answered my question I realize it is this one that cause my confusion "You end it with an interact statement, which conncets your console to the stdin/stdout of the process you spawned. The redis-server program is not interactive: it doesn't accept any console input." 在@larsks回答了我的问题之后,我意识到这是引起我困惑的原因:“您以一个交互语句结束它,这使您的控制台与所产生的进程的stdin / stdout一致。redis-server程序不是交互式的:它不接受任何控制台输入。”

I check the code again and found it was this code that made me think the process was stuck 我再次检查该代码,发现正是该代码使我认为该过程卡住了

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

I never saw redis-cli run. 我从未见过redis-cli运行。

But if I change to 但是如果我改变为

#!/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.

It works as I expected. 它按我的预期工作。

BTW the reason I use expect here is first to make sure redis server starts then delete some keys. 顺便说一句,我在这里使用期望的原因是首先确保Redis服务器启动,然后删除一些密钥。

What do you expect the first example to do? 您期望第一个示例做什么? You end it with an interact statement, which connets your console to the stdin/stdout of the process you spawned. 您以一个interact语句结束它,该语句将您的控制台连接到您生成的进程的stdin / stdout。 The redis-server program is not interactive: it doesn't accept any console input. redis-server程序不是交互式的:它不接受任何控制台输入。 When you run redis-server , it will get as far as... 当您运行redis-server ,它将达到...

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

...and then it stops, waiting for redis clients to connect and operate on it. ...然后停止,等待redis客户端连接并对其进行操作。 Also, note that the Redis version I'm using ends with Ready to accept connections rather than The server is now ready to accept connections , so I'll be using that in the following examples. 另外,请注意,我正在使用的Redis版本以Ready to accept connections结束Ready to accept connections而不是The server is now ready to accept connections ,因此在以下示例中将使用它。

We can add a puts command to the expect script to see that it isn't actually getting stuck anywhere. 我们可以在期望脚本中添加一个puts命令,以查看它实际上并没有卡在任何地方。 If I run the following: 如果我运行以下命令:

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

I get as output: 我得到的输出:

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

So we can see that it's not getting stuck at the spawn statement, nor even at the expect statement. 因此,我们可以看到它并没有卡在spawn语句,甚至没有在expect语句。

What's not clear from your question is why you're even using expect in this situation, since redis-server is not an interactive program and does not produce any prompts that require automation. 什么是不是从你的问题清楚的是为什么你甚至使用expect在这种情况下,因为redis-server不是一个交互程序,并不会产生需要自动化的任何提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM