Search code examples
linuxshexpectspawn

Expect executed shell script don't exit


I using expect to automate the installation of a software. This is my simplified expect code that I have in my script, but the same problem also happens when I paste it directly in console.

expect <<-EOS
#!/usr/bin/expect -f
set timeout -1

#run the command
spawn ./installSoftware.sh

expect "*?Question 1*" { send "N\r" }

expect -ex {Question 2 (default is [RANDOM STRING]):} { send "\r" }

puts "-------------- DONE 1 ---------------"
expect eof
puts "-------------- DONE 2 ---------------"
EOS

the installSoftware.sh is a big script that installs packages and many other things. The script have in the last line.

echo "----------- Done-----------"

The installSoftware.sh scripts works fine when I execute it directly, but when I use it with the expect script, it runs through and show me "done" echo from the last line, but then it stucks. I can see with HTOP that the process of the script is still running, the installSoftware.sh script don't exit.

Any ideas how to debug this? Is this more a problem of the installSoftware.sh script or something related to expect?

What I can tell is, if I remove this line from the installSoftware.sh, it exit my installSoftware.sh as expected, but this is somewhere in the middle of the hugh script.

rpm -Uvh --oldpackage --replacepkgs mysoftware.rpm 

Solution

  • I have found the issue, the problem were inside the RPM package in a script. This example replicate the same issue and will show that the spawn task will stuck at the end without direct indication of a child process.

    expect.sh

    #!/bin/bash
        
    expect <<-EOS
    #!/usr/bin/expect -f
    set timeout -1
        
    #run the command
    spawn ./test.sh
        
    expect eof
        
    EOS
    

    test.sh

    cp -f sleep.sh /tmp
    chmod 777 /tmp/sleep.sh
    su testuser -c "/tmp/sleep.sh &"
    echo "--- DONE ---"
    

    sleep.sh

    #!/bin/bash
    
    while true
    do
       echo "loading..."
       sleep 5
    done