2

I have a NodeJS app that I want to start with Upstart. So far the process works, Upstart initiates the app, but when I load the page, the CSS are not loading. The curious thing is that if I start the app using #node myapp.js, it will start and load the files correctly. I can't the difference, if any, that makes this issue possible. ANy help would be appreciated.

This is how I've set up the app:

var express = require('express'),
    config = require('getconfig'),
    path = require('path'),
    expressValidator = require('express-validator'),
    crypto = require('crypto'),
    app = express();

app.configure('development', function () {
    app.use(express.logger('dev'));
});

app.configure(function () {
    app.use(require('less-middleware')({ src: path.join(__dirname, 'public') }));
    app.use(express.static('public'));
    require('./bootstraps/handlebars')(app);
    app.use(express.favicon());
    app.use(express.limit('5mb'));
    app.use(express.urlencoded({limit: '5mb'}));
    app.use(express.multipart({limit: '5mb'}));
    app.use(express.json({limit: '5mb'}));
    app.use(express.cookieParser(config.http.cookieSecret));
    app.use(expressValidator());
    app.use(express.methodOverride());
    require('./bootstraps/session')(app);
    app.use(function(req,resp,next){
        resp.locals.session = req.session;
        next();
    });
    app.use(app.router);
    require('./bootstraps/controllers')(app);
});

app.listen(process.env.PORT || config.http.port);

module.exports = app;

... and this is my Upstart script:

#!upstart
description "My Project"
author      "Author"

start on startup
stop on shutdown

script
    export HOME="/root"

    echo $$ > /var/run/myprocess.pid
    su -s /bin/sh -c 'exec "$0" "$@"' theuser -- /root/local/bin/node /path/to/my_project/myapp.js >> /var/log/my_project/system.log 2>&1
end script

pre-start script
    # Date format same as (new Date()).toISOString() for consistency
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/my_project/system.log
end script

pre-stop script
    rm /var/run/dreamcasino.pid
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/my_project/system.log
end script
Reydel Leon
  • 1,035
  • 2
  • 11
  • 34

1 Answers1

3

You can use chdir so that the working directory is correct

script
    export HOME="/root"
    chdir /path/to/my_project/
    echo $$ > /var/run/myprocess.pid
    exec sudo -u theuser /root/local/bin/node /path/to/my_project/myapp.js >> /var/log/my_project/system.log 2>&1
end script
leszek.hanusz
  • 5,152
  • 2
  • 38
  • 56
  • I'm getting this in the log: 'su: user /root/local/bin/node does not exist'. haven't changed anything, only added the line suggested. – Reydel Leon Jan 27 '14 at 14:31
  • Yes, and the result is in the previous comment. Thanks for your time by the way. – Reydel Leon Jan 27 '14 at 14:49
  • This worked for me, but I'd love to know why. Can you explain why we would need to manually chdir? Does this have to do with how $HOME is set? Thanks! – Kyle Chadha Jan 05 '15 at 21:38
  • I'm selecting the answer as correct taking into account that it worked for @KyleChadha. I quit that project, so I could not verify if it would finally work on my case. – Reydel Leon Jan 06 '15 at 04:38