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

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:

#!/bin/bash

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:
 /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');'
 /var/lib/openshift/54e408fbe0b8cd1f400000a9/app-root/runtime/repo//.openshift/cron/daily/cron.sh:
 [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?

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 cron.sh is being executed.

To resolve the problem, get rid of cron.sh 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

Attribution
Source : Link , Question Author : UnbrandedTech , Answer Author : Michael Hampton

Leave a Comment