title: Executing a remote bash script that contains a while loop through ssh only runs once and does not loop
body: <p>writing a script to query some scraping jobs I am doing on a remove machine. It works fine when I execute it on the remote machine directly, looping and counting how many scrapers are running and how many are idle BUT when I try to run it through a ssh command it only runs once and then quits. I want it to keep looping till all jobs are done (then I can have Jenkins execute another operation.</p>
  <p>Here is the script I am running that works fine when I am remote:</p>
  <pre><code>#/bin/bash
  
  SCRAPERS=$(dir /app/data/scraper/)
  
  
  echo 'Checking for running jobs...'
  echo ''
  num_running=1
  num_idle=0
  
  while [ $num_running -gt 0 ]
  do
          num_idle=0
          num_running=0
  for s in $SCRAPERS
  do
    RUNNING=$(ps x | grep scrapy | grep &quot;$s&quot; | grep -v grep | awk '{print $2}')
    if [ -z $RUNNING ]; then
            echo &quot;IDLE: $s&quot;
            num_idle=$(expr $num_idle + 1)
    else
      t=$(ps -p 86958 -o etime | tail -n1 | awk '{print $1}')
      echo &quot;RUNNING: $s ($t)&quot;
      num_running=$(expr $num_running + 1)
    fi
  
  done
  
  #echo 'Number Running: ' $num_running
  #echo 'Number Idle: ' $num_idle
  
  echo 'still running..'
  sleep 5
  done
  </code></pre>
  <p>That works great and keeps going but when i try something like this it runs once and then breaks out immediately afterwards.</p>
  <pre><code>ssh -n name@server 'cd /app/scripts/ &amp;&amp; /app/scripts/scrape_status.sh'
  </code></pre>
  <p>Thanks for anything thoughts or insights on how to make this work.</p>