enguerranws
enguerranws

Reputation: 8233

Running Node as Cron task

I have a simple question. I try to run a Node JS program on a Cron task via a bash script.

So, on crontab -e, I made a task @reboot that execute boot.sh :

# m h  dom mon dow   command
@reboot bash /home/pi/boot.sh

And my bash script :

#!/bin/sh
set -e
cd /home/pi/Sites/node-raspberry-pi/
/usr/bin/git pull
node /home/pi/Sites/node-raspberry-pi/index.js 3000 # where 3000 is the argument of my program
exit 0

When I do bash /home/pi/boot.sh, it works as supposed.

What do I miss ?

Note : both crontab -e and bash /home/pi/boot.sh are exectued as pi user.

Upvotes: 0

Views: 1808

Answers (1)

Karel Kubat
Karel Kubat

Reputation: 1675

Might be that your node cannot be found when cron is running; because cron has a limited search path. Try prefixing it with wherever you have node installed, so e.g., instead of

node /home/pi/Sites/node-raspberry-pi/index.js 3000

you would get

/usr/local/bin/node /home/pi/Sites/node-raspberry-pi/index.js 3000

You can also extend the searchpath for cron, see man 5 crontab. Hope this helps..

Upvotes: 4

Related Questions