Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I made a rather simple startup script for my node.js program that should run on startup:

start on startup
stop on shutdown

script
    exec sudo -u max WEBSITES_DIR=/home/max/websites/ /usr/local/bin/node /home/max/websites/server.js 2>&1 >> /var/log/node.log
end script

If I use:

sudo start my-program 

it works, but when I reboot the machine I get the error message:

init: my-program main process (325) terminated with status 2

Anybody any idea on how to debug this?

share|improve this question
up vote 11 down vote accepted

The startup event is the very first event that gets emitted in the Upstart bootup process. There are all kinds of things that won't have happened yet - the root filesystem will still be mounted read-only, networking hasn't been initialized, etc.

I suspect that the job is exiting quickly because all of its dependencies (explicit or implicit) aren't initialized yet. I'd change the start condition to something like

start on filesystem and started networking

At least then you'll be able to write to your logfile.

As a side note, it looks like you reversed your output redirection arguments - you actually meant >> /var/log/node.log 2>&1. Using 2>&1 means "take whatever is currently fd 1, and make fd 2 the same thing", but you haven't changed what stdout is yet.

share|improve this answer
    
Thats a good hint, thanks! Will try it out tomorrow. The script I am executing is on a mounted drive (the server is a virtual machine on my development pc) – is that already mounted when the filesystem is ready? Thanks a lot for the note on the redirection arguments. :-) – Max Jun 13 '11 at 23:22
    
That worked! awesome, thanks a lot :-)))) – Max Jun 14 '11 at 8:05
    
The "filesystem" event is emitted when all filesystems listed in /etc/fstab (and also /lib/init/fstab) have been mounted. There are also events emitted for each filesystem, and certain classes of filesystem. The upstart-events(7) manpage has more information about the different standard events. It's new in Natty, so if you don't have that, you can read it with crappy formatting at manpages.ubuntu.com/manpages/natty/man7/upstart-events.7.html – Evan Broder Jun 15 '11 at 22:48
    
In case anyone comes across this question from a virtual machine environment such as vagrant, use start on virtual-filesystems. This will wait for things like network mounts. (Ref: manpages.ubuntu.com/manpages/trusty/man7/…) – msanford Dec 8 '15 at 21:40

HiMax,

Startup node.js on Ubuntu server

more /etc/init/noded.conf

Ubuntu upstart file at /etc/init/noded.conf

description "noded.conf"

author "ntbinh"

start on runlevel [2345]

stop on runlevel [06]

respawn

script

su - ubuntu -c "NODE_ENV=test exec sudo /etc/init.d/noded start"

end script

======================================================

  1. You have already /etc/init.d/noded

  2. You can edit /etc/init/noded.conf ==> startup booting:

    Ubuntu upstart file at /etc/init/noded.conf

    description "noded.conf" author "ntbinh"

    respawn

    start on runlevel [2345]

    stop on runlevel [06]

    script

    su - ubuntu -c "NODE_ENV=test exec sudo /usr/bin/nohup /usr/bin/node /home/ubuntu/server.js" >> /home/ubuntu/log.log

    end script

share|improve this answer

In my case, the similar error was caused by trying to start a script from my upstart script, that didn't actually exist. It turned out that I forgot to commit and push the script.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.