2

I know this is a hugely popular type of question so I apologise for that, but I'm truly stuck and I've read through the cron wiki and tried all those suggestions.

I've also tried everything this guy lists in his question, namely:

  • adding bash path to crontab
  • specifying full path to processes inside bash script
  • prefixing the script inside crontab with /bin/bash
  • sweating blood since I have a wedding to go to and desperately need this damn script to just work

I can confirm the script works when running from my terminal.

So here goes...

Current crontab:

# add bash to cronjob path
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# start litecoin daemon on boot
@reboot /opt/litecoin-0.14.2/bin/litecoind

# check every 2 minutes to see if block scrape running and restart it if not
*/2 * * * * /home/grayedfox/github/blockscrape/restartBlockscrape.sh

Script:

#!/bin/bash

NODEPATH=$(which node)
PROCESS="$NODEPATH /home/grayedfox/github/blockscrape/main.js"

if pgrep -f "$PROCESS" > /dev/null; then
  echo "Blockscrape is doing it's thing - moving on..."
else
  echo "Blockscrape not running! Starting again..."
  $PROCESS
fi

I would expect, even if the job fails, to see some sort of error somewhere but I get nothing and see nothing!

Update: I can actually see the cron job being run by checking the /var/log/syslog output.

The output is:

CRON[15038]: (grayedfox) CMD (/home/grayedfox/github/blockscrape/restartBlockscrape.sh)
CRON[15037]: (CRON) info (No MTA installed, discarding output)

The script also sits inside the directory of the node script it calls (although I specify full path as I understand the script, when run by cron, has it's pwd as $HOME).

GrayedFox
  • 191

1 Answers1

0

Although all the comments were helpful to was @muru's suggestion to log the result of the script which helped me the most - so make sure to give that one an upvote!

I ended up logging the result of each operation inside the script which immediately made it clear the issue was not cron running the job but rather that node was not available to the cron environment. I also updated my crontab to make the shell default to bash but this was not necessary to have the job run.

This final script works however it is still unclear to me why line 3 fails for cron but succeeds in a bash terminal, I will ask a new question for that particular issue.

Crontab:

# make default shell BASH
SHELL=/bin/bash

# start litecoin daemon on boot
@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

Bash script:

#!/bin/bash

NODE="$(which node)" # <-- output in terminal is as expected (gets node version in use) but 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"

# source $HOME/.bashrc # <-- even after sourcing .bashrc, line 3 returns nothing

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 "Nodepath: $NODE" >> $LOGFILE
  echo "Process: $PROCESS" >> $LOGFILE
  $PROCESS >> $LOGFILE
fi
GrayedFox
  • 191