title: Spawn id exp7 not open while executing error shell script
body: <p>I am using cron to run a script that connects to another server via sftp and uploads files. Once in a while, I will receive the error: <strong>Spawn-id-exp7-not-open while executing  send \"ls\r\"</strong> and the script will stop executing (files will not be sent). However, most of the time, the script will run with no errors.</p>
  
  <p>Below is my script I use to connect :</p>
  
  <pre><code>#!/bin/bash
  HOST="removed.com:\API"
  USER="user"
  PASS="removed"
  
  VAR=$(expect -c "
    spawn sftp -o \"BatchMode no\" -b /var/www/prep/cmd -P 13266 $USER@$HOST
  
    expect \"Password:\"
    send \"$PASS\r\"
  
    expect \"\\\\$\"
    send \"ls\r\"
  
    expect -re \"$USER.*\"
    send \"exit\"
  ")
  
  
  echo "==============="
  echo "$VAR"
  </code></pre>
  
  <p>Below is the script that runs after I connect (cmd) :</p>
  
  <pre><code>put "/var/www/prep/1.xml"
  put "/var/www/prep/2.xml"
  </code></pre>
  
  <p>I took a look at a similar question found at <a href="https://stackoverflow.com/questions/18812597/send-spawn-id-exp7-not-open">send: spawn id exp7 not open</a> , but it has not helped me. Perhaps the connection ends before the cmd script can run?</p>
  
  <p>Thanks for your help!</p>