title: Why doesn't $(which node) work inside a cron job executing a BASH shell?
body: <p>According to <a href="https://stackoverflow.com/a/214503/3249501">this answer</a> the reason cron doesn't have access to environment variables normally associated with a BASH terminal is because it doesn't source the users .bashrc file.</p>
  
  <p>I have a script which does source my .bashrc file but it still fails to find my currently in use version of Node (meaning I need to list the full directory and change it with every update!).</p>
  
  <p>Script:</p>
  
  <pre><code>#!/bin/bash
  
  source $HOME/.bashrc # &lt;-- even after sourcing .bashrc, '$(which node)' returns nothing
  
  NODE="$(which node)" # &lt;-- output is blank in cron job
  PROCESS="/home/grayedfox/.nvm/versions/node/v8.9.4/bin/node /home/grayedfox/github/blockscrape/main.js"
  LOGFILE="/tmp/log.out"
  
  export BLOCKSCRAPECLI="/opt/litecoin-0.14.2/bin/litecoin-cli"
  
  if pgrep -f "$PROCESS" &gt; /dev/null; then
    echo "Blockscrape is doing it's thing - moving on..." &gt;&gt; $LOGFILE
  else
    echo "Blockscrape not running! Starting again..." &gt;&gt; $LOGFILE
    echo "Process: $PROCESS" &gt;&gt; $LOGFILE
    echo "Node: $NODE" &gt;&gt; $LOGFILE # &lt;-- outputs only 'Node: ' in log file
    $PROCESS &gt;&gt; $LOGFILE
  fi
  </code></pre>
  
  <p>Crontab:</p>
  
  <pre><code># make default shell BASH
  SHELL=/bin/bash
  
  # reboots litecoin daemon if it dies
  @reboot /opt/litecoin-0.14.2/bin/litecoind
  
  # check every minute to see if block scrape running and restart it if not
  * * * * * /home/grayedfox/github/blockscrape/restartBlockscrape.sh
  </code></pre>
  
  <p>I can confirm that node (and doing "which node") works fine in my terminal.</p>
  
  <p>Thanks for the help!</p>