I am currently trying to set up a cronjob that will start a node.js server whenever that server is down.

For that purpose, I have written a simple script to test and start the node server.


ts=$(date +%T)

if pgrep -f "node"
    echo $ts": node running"
    echo $ts": node not running"
    '/usr/bin/node' '/home/pi/project/serveronly/index.js' > '/home/pi/project/node.log'

The corresponding crontab is:

m   h   dom   mon   dow   command
*   *    *     *     *    /home/pi/project/sanity_check.sh >> /home/pi/project/cron.log 2&>1

my logging shows the following: If I simply start the script it will start the server and log the node.js output properly.

For the script started by cron it looks this way: If an instance of node.js is currently running it will detect that and log accordingly. If none is detected it will log into cron.log properly, but it will log nothing into node.log and it will not start a server.

  • What is the pwd inside cron? Please add it to the question body. Why don't you add cd / to the script? – kubanczyk Jul 5 '18 at 12:33
  • What do you mean by pwd? (as for the cd, I will try that out. It should help me to avoid some mistakes. Thank you) – Junge Jul 5 '18 at 12:47
  1. You're not providing the full path to /home/pi/project/serveronly/index.js -- you're missing the initial forward-slash, so the node program is failing to find your script.

  2. the command is successfully overwriting a file under your home directory, home/pi/project/node.log; presumably that's what you meant with project.log. If you want to append to that log, use two greater-than signs, not one.

I would use this, instead:

/usr/bin/node /home/pi/project/serveronly/index.js >> /home/pi/project/node.log
| improve this answer | |
  • Thank you. this explains the "weird" logging behaviour. Unfortunately the missing forward-slash is just an error I made while copying. I edited my question accordingly. – Junge Jul 5 '18 at 12:44

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.