title: Shell Script not running properly
body: <p>I have a linux shell script that when run from command line works perfectly but when scheduled to run via crontab, it does not give desired results.</p>
  
  <p>The script is quite simple, it checks to see if mysql-proxy is running or not by checking if its pid is found using the pidof command. If found to be off, it attempts to start the proxy.</p>
  
  <pre><code># Check if mysql proxy is off
  # if found off, attempt to start it
  
  if pidof mysql-proxy 
  then
      echo "Proxy running."
  else
      echo "Proxy off ... attempting to restart"
      /usr/local/mysql-proxy/bin/mysql-proxy -P 172.20.10.196:3306 --daemon --proxy-backend-addresses=172.20.10.194 --proxy-backend-addresses=172.20.10.195
  
      if pidof mysql-proxy
      then
          echo "Proxy started"
      else
          echo "Proxy restar failed"
      fi
  fi
  echo "==============================================="
  </code></pre>
  
  <p>The script is saved in a file <strong>check-sql-proxy.sh</strong> and has permissions set to <strong>777</strong>. When I run the script from command line (sh check-sql-proxy.sh) it gives the desired output.</p>
  
  <pre><code>4066
  Proxy running.
  ===============================================
  </code></pre>
  
  <p>The script is also scheduled to run every 5 minutes in crontab as </p>
  
  <pre><code>*/5 * * * * bash /root/auto-restart-mysql-proxy.sh &gt; /dev/sql-proxy-restart-log.log
  </code></pre>
  
  <p>However, when I see the sql-proxy-restart-log.log file it contains the output:</p>
  
  <pre><code>Proxy off ... attempting to restart
  Proxy restar failed
  ===============================================
  </code></pre>
  
  <p>It seems that <strong>pidof</strong> command fails to return the pid of the running application which brings the flow of script in else condition.</p>
  
  <p>I am unable to figure out how to resolve this since when I run the script manually, it works fine. </p>
  
  <p>Can anyone help what I am missing with regards to permissions or settings?</p>
  
  <p>Thanks in advance.
  Mudasser</p>