gpt4 book ai didi

shell - Expect 中的后台生成进程

转载 作者:行者123 更新时间:2023-12-01 17:55:33 50 4
gpt4 key购买 nike

我正在使用 Expect 在我的服务器上启动应用程序:

#!/usr/bin/expect

set timeout -1

spawn "bin/start-all.sh"
expect {
-re "Found MongoDB in" { send "y\r"; exp_continue }
-re "Found Hadoop in" { send "y\r"; exp_continue }
-re "Going to start Hadoop" { interact }
}

在脚本运行时,我可以在几秒钟内访问服务器上的应用程序,但一旦脚本结束,应用程序就会变得不可用。

我在 Debug模式下运行expect并在最后得到以下输出:

expect: does "vendors area. Do you want to start it? [y/n] y\r\n" (spawn_id exp6) match regular expression "Found MongoDB in"? Gate "Found MongoDB in"? gate=no
"Found Hadoop in "? Gate "Found Hadoop in "? gate=no
"Going to start Hadoop"? Gate "Going to start Hadoop"? gate=no
Going to start Hadoop...

expect: does "vendors area. Do you want to start it? [y/n] y\r\nGoing to start Hadoop...\r\n" (spawn_id exp6) match regular expression "Found MongoDB in"? Gate "Found MongoDB in"? gate=no
"Found Hadoop in "? Gate "Found Hadoop in "? gate=no
"Going to start Hadoop"? Gate "Going to start Hadoop"? gate=yes re=yes
expect: set expect_out(0,string) "Going to start Hadoop"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "vendors area. Do you want to start it? [y/n] y\r\nGoing to start Hadoop"
tty_raw_noecho: was raw = 0 echo = 1
interact: received eof from spawn_id exp6
tty_set: raw = 0, echo = 1
tty_set: raw = 5, echo = 0

我尝试过使用exit 0interactexp_continuedisconnectsleep 10 在最后一个模式下,以及期待 eof 但似乎没有任何效果。我也尝试过运行 expect start-all.exp & 但这也不起作用。

当我手动运行 bin/start-all.sh 时,脚本会启动必要的进程,然后退出。然而,预期这些进程似乎被杀死了。我该如何解决这个问题?

最佳答案

我也遇到了同样的问题,并解决了这个问题。

expect退出时,它会向生成的子进程发送一个SIGHUP(挂断信号)。默认情况下,此 SIGHUP 会导致生成的进程终止。

如果您希望底层进程不因SIGHUP而终止,您有两个简单的选择。两者都工作得很好:

1) 要求 expect 使底层进程忽略 spawn 行中的 SIGHUP,如下所示:

#!/usr/bin/expect -f
...
spawn -ignore HUP command args...
...
expect_background

2)自己动手 - 忽略底层进程本身中的SIGHUP:

这是演示方法 2 的工作脚本:

#!/usr/bin/expect -f
#
# start a process and background it after it reaches a certain stage
#
spawn perl -e "\$SIG{HUP} = 'IGNORE'; for (\$a='A';; \$a++) {print qq/value is \$a\\n/; sleep 1;}"

set timeout 600

# Detailed log so we can debug (uncomment to enable)
# exp_internal -f /tmp/expect.log 0

# wait till the subprocess gets to "G"
expect -ex "value is G"

send_user "\n>>> expect: got G\n"

# when we get to G, background the process
expect_background

send_user ">>> spawned process backgrounding successful\n"
exit 0

这是一个运行示例:

$ ./expect-bg
spawn perl -e $SIG{HUP} = 'IGNORE'; for ($a='A';; $a++) {print qq/value is $a\n/; sleep 1;}
value is A
value is B
value is C
value is D
value is E
value is F
value is G

>>> expect: got G
>>> spawned process backgrounding successful

正如 ps 输出中所预期的那样,perl 进程处于后台且处于事件状态。

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
hankm 6700 0.0 0.0 17696 2984 ? Ss 18:49 0:00 perl -e $SIG{HUP} = 'IGNORE'; for ($a='A';; $a++) {print qq/value is $a\n/; sleep 1;}

关于shell - Expect 中的后台生成进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17916201/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com