There's a relatively low limit on the number of simultaneous programs that can be spawned at once (it depends on how many virtual terminals your system supports; I'm actually surprised that you got to over 1000 thereā¦) so you need reap those old programs once you're done with them (Expect does reap everything on exit, but here it matters because you're running out much sooner than that). What's more, the limit will depend on what else is going on on your system, as virtual terminals are actually a system-global resourceā¦
To reap the old program once you're done with it, add wait to the end of your loop (assuming you don't want the subprocess to continue past the end of the loop, of course) to get this:
foreach temp $list {
spawn -X $temp
while {1} {
expect {
eof {break}
"password" {send "password\r"}
"\]" {send "exit\r"}
}
}
wait ;#### <<<<<----- THIS!!!
}
You might also want to take a look at exp_continue, as that lets you rewrite to get rid of the explicit while (and also the need to explicitly handle the EOF condition) and overall make your code simpler:
foreach temp $list {
spawn -X $temp
expect {
"password" {send "password\r"; exp_continue}
"\]" {send "exit\r" ; exp_continue}
}
wait
}