openshift, cron, node, line 1: syntax error near unexpected token `('

UnbrandedTech asked:

I’m pretty sure this is some kind of config problem, or something like that.
I’m using node V 0.10 and the cron cartridge V 1.4.

Here’s my bash script to run my node script:


node ${OPENSHIFT_REPO_DIR}.openshift/cron/daily/cron.js

and here is my node script

var mongoose    = require('mongoose');
var express     = require('express');
var moment      = require('moment');
var _                   =   require('lodash');
var D               = require('d.js');
var events = require('events');
var eventEmitter = new events.EventEmitter();

The error in the log is

 Thu Mar 19 03:45:35 EDT 2015: START daily cron run
 /var/lib/openshift/54e408fbe0b8cd1f400000a9/app-root/runtime/repo//.openshift/cron/daily/cron.js: line 1: syntax error near unexpected token `('
 /var/lib/openshift/54e408fbe0b8cd1f400000a9/app-root/runtime/repo//.openshift/cron/daily/cron.js: line 1: `var mongoose    = require('mongoose');'
 [Error: /var/lib/openshift/54e408fbe0b8cd1f400000a9/app-root/runtime/repo/node_modules/mongoose/node_modules/mongodb/node_modules/bson/build/Release/bson.node: invalid ELF header]
 js-bson: Failed to load c++ bson extension, using pure JS version
 Thu Mar 19 03:46:11 EDT 2015: END daily cron run - status=0

I know by this the cron is running node because it would never run this file without bash invoking it. So is it a param issue? Am I missing something obvious?

My answer:

OpenShift runs every file in the .openshift/cron/* directories on the relevant schedule.

Thus, we see that first cron.js is being executed by bash and throwing a syntax error. Then immediately afterward is being executed.

To resolve the problem, get rid of and add this to the first line of cron.js:

#!/usr/bin/env node

var mongoose = ...

The hashbang tells Linux to execute the script with node rather than the default shell.

And remember that the script must be executable before you check it in (but I think you’ve already done this).

chmod +x .openshift/cron/daily/cron.js

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.