根据this answer,cron无法访问通常与BASH终端相关联的环境变量的原因是因为它不会提供用户.bashrc文件。
我有一个脚本来源我的.bashrc文件,但它仍然无法找到我当前使用的Node版本(这意味着我需要列出完整的目录并在每次更新时更改它!)。
脚本:
#!/bin/bash
source $HOME/.bashrc # <-- even after sourcing .bashrc, '$(which node)' returns nothing
NODE="$(which node)" # <-- 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" > /dev/null; then
echo "Blockscrape is doing it's thing - moving on..." >> $LOGFILE
else
echo "Blockscrape not running! Starting again..." >> $LOGFILE
echo "Process: $PROCESS" >> $LOGFILE
echo "Node: $NODE" >> $LOGFILE # <-- outputs only 'Node: ' in log file
$PROCESS >> $LOGFILE
fi
crontab的:
# 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
我可以确认该节点(和#34;哪个节点&#34;)在我的终端中正常工作。
感谢您的帮助!
答案 0 :(得分:0)
正如this answer中所讨论的那样 - 这个答案问题是在Ubuntu 16.04(以及许多以前的版本)中,标准的.bashrc文件如果没有以交互方式运行则退出。
从我的bash文件中注释掉下面的代码修复了它:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac