Upstart / init script not working
Asked Answered
O

2

8

I'm trying to create a service / script to automatically start and controll my nodejs server, but it doesnt seem to work at all.

First of all, I used this source as main reference http://kvz.io/blog/2009/12/15/run-nodejs-as-a-service-on-ubuntu-karmic/

After testing around, I minimzed the content of the actual file to avoid any kind of error, resulting in this (the bare minimum, but it doesnt work)

description "server"
author      "blah"

start on started mountall
stop  on shutdown

respawn
respawn limit 99 5

script
  export HOME="/var/www"

  exec nodejs /var/www/server/server.js >> /var/log/node.log 2>&1
end script

The file is saved in /etc/init/server.conf

when trying to start the script (as root, or normal user), I get:

root@iof304:/etc/init# start server
start: Job failed to start

Then, I tried to check my syntax with init-checkconf, resulting in:

$ init-checkconf /etc/init/server.conf 
File /etc/init/server.conf: syntax ok

I tried different other things, like initctl reload-configuration with no result.

What can I do? How can I get this to work? It can't be that hard, right?

Octet answered 27/2, 2014 at 13:26 Comment(9)
and you're sure that you have a file at /usr/local/bin/nodejs?Superphosphate
@MattPileggi Damn, you're right... it's missing. Hm, strange, I remember using nodejs that way - let me try something else. Result: no change - I changed the command directly to nodejs, and even removed the sudo -u part to execute as root - nothing changed. I tried to call the command directly over console - that works.Octet
Sorry not sure what else to look at. I have all of my init scripts setup to use Forever - which takes care of keeping it running. Did you look into that at all?Superphosphate
Anything in /var/log/messages or boot.log about the job failed message??Peripatetic
Any updates, I'm getting this too!Flyover
Just got the same problem. The script executes fine manually. The upstart job just fails though and leaves no traces (logs) unfortunately.Molality
Any updates on this? I've runned into the same problem trying to demonize Faye. Same error, no log traces as well!Remanence
Is there anything in /var/log/upstart/ ?Lucianaluciano
is this still unsolved?Catholic
L
0

This is what our typical startup script looks like. As you can see we're running our node processes as user nodejs. We're also using the pre-start script to make sure all of the log file directories and .tmp directories are created with the right permissions.

#!upstart
description "grabagadget node.js server"
author      "Jeffrey Van Alstine"

start on started mysql
stop on shutdown
respawn

script
    export HOME="/home/nodejs"
    exec start-stop-daemon --start --chuid nodejs --make-pidfile --pidfile /var/run/nodejs/grabagadget.pid --startas /usr/bin/node -- /var/nodejs/grabagadget/app.js --environment production >> /var/log/nodejs/grabagadget.log 2>&1
end script

pre-start script
    mkdir -p /var/log/nodejs
    chown nodejs:root /var/log/nodejs
    mkdir -p /var/run/nodejs
    mkdir -p /var/nodejs/grabagadget/.tmp

    # Git likes to reset permissions on this file, but it really needs to be writable on server start 
    chown nodejs:root /var/nodejs/grabagadget/views/layout.ejs
    chown -R nodejs:root /var/nodejs/grabagadget/.tmp

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

pre-stop script
    rm /var/run/nodejs/grabagadget.pid
    sudo -u nodejs echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/nodejs/grabgadget.log
end script
Lucianaluciano answered 21/6, 2017 at 21:48 Comment(0)
H
-1

As of Ubuntu 15, upstart is no longer being used, see systemd.

Harmattan answered 25/8, 2016 at 18:3 Comment(1)
I don't know why this has been voted down. It solved the issue for me. The other answer above applies to a superseded system, anyone setting up a new system needs to know this no longer applies.Eugeniusz

© 2022 - 2024 — McMap. All rights reserved.