0

I have an sh script I want called every three hours via cron. The script calls a bunch of node.js scripts and works perfectly when called directly. However, when called via cron, I get a date logged by the sh script but none of the other logs from my node.js scripts (which all log fine when the sh is called directly). Any idea why?

The script is located in an interior directory but I'm using all absolute paths. Please find my code below and more details on the node.js stuff if necessary on Github (NB: the server was recently changed from sh to bash but I don't think this should have any real impact).

Crontab

SHELL=/bin/sh
MAILTO=my@email.com
0 */3 * * * /absolute/path/to/script.sh

script.sh

#!/usr/bin/env sh
node /absolute/path/to/script1.js
node /absolute/path/to/script2.js
node /absolute/path/to/script3.js

LOGFILE=/absolute/path/to/error.log

log(){
    message="$@"
    echo $message
    echo $message >>$LOGFILE
}

log "Cron performed $(date)"
Primus202
  • 648
  • 7
  • 24
  • PATH from crond must not be OK for your node commands – Stephane Rouberol Aug 22 '12 at 21:58
  • 1
    Don't think so. If I call `bash /absolute/path/to/script.sh` with the path copied directly from `crontab -e` it works fine. – Primus202 Aug 22 '12 at 22:01
  • 3
    Try to specify full path to `node` as well. – choroba Aug 22 '12 at 22:20
  • 1
    @Primus202, but your script does not use bash, and even if it did, it would not use your own environment. Use the full path to `node` – glenn jackman Aug 22 '12 at 23:10
  • Tried full path (i.e `/usr/local/bin/node /absolute/path/to/twitterupdate.js`), still no dice. – Primus202 Aug 23 '12 at 13:39
  • Does `node` need an environment variable or simething in order to find its libraries, etc? It's not just `PATH` which is different, it's the entire environment. See also http://stackoverflow.com/questions/819944/proper-way-to-run-a-script-using-cron – tripleee Aug 23 '12 at 17:13

2 Answers2

2

Crontab does not load your normal user environment when it invokes a command, so any other environment variables your bash script or node.js stuff depends on won't be there. For example, how does your shell find the 'node' command?

Cron scripts do not run in your user account's bash environment, so running them directly in your shell is not a valid test.

Simon Hibbs
  • 5,941
  • 5
  • 26
  • 32
  • I've tried using all full paths, even for node, and still no luck. `/usr/local/bin/node /absolute/path/to/twitterupdate.js` – Primus202 Aug 23 '12 at 13:43
  • You could try having a look through your environment for anything node might depend on, any other environment settings. There must be something. – Simon Hibbs Aug 24 '12 at 07:00
  • The solution was using absolute paths in all my file includes within my node scripts as well so nearly. Thanks. – Primus202 Aug 24 '12 at 21:10
0
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
SHELL=/bin/sh
MAILTO=my@email.com
0 */3 * * * /absolute/path/to/script.sh
Satish
  • 16,544
  • 29
  • 93
  • 149