1

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).

  • 2
    Where would you expect to see an error? unless you have an MTA configured, the standard output to which your echo statements go, and anything on the standard error stream, will be discarded AFAIK – steeldriver Mar 16 '18 at 0:19
  • 1
    Might be a classic everybody-forgets-that-cron-runs-headless issue. Does litecoind require a $DISPLAY ? The script seems to - echo to where? Consider having it log instead. – user535733 Mar 16 '18 at 0:37
  • 1
    "# reboots litecoin daemon if it dies" ... no, it runs litecoin on system reboot, that's all. – muru Mar 16 '18 at 1:22
  • 1
    Use /bin/bash -x /home/grayedfox/github/blockscrape/restartBlockscrape.sh > /tmp/log.out 2>&1 as the cron command and post the contents of /tmp/log.out. – muru Mar 16 '18 at 8:08
  • 1
    Here is how to emulate cron: serverfault.com/a/85906/430340. So make your script work with it, then add it to the crontab. If this still not work, configure a MTA so cron can send it's output to somewhere. – pim Mar 16 '18 at 8:25
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.