2

I am trying to write an upstart script for my ubuntu machine, which is version 8.04 "Hardy". I have followed the instructions on this site: upstart for node.js but it seems like these instructions are for a current version of ubuntu.

I noticed that the /etc/init directory does not exist on my machine, first I tried putting the script in the /etc/init.d directory and then I created the /etc/init dir and placed it there.

I will post my upstart script below (which is basically the same as from the website above with some path changes), but when I run start jobname, I just get an error "start: Unknown job: jobname". So then I changed the script around to a slimmed down version, posted below, and still I get the same result.

For now, I am using the 'nohup' command to run my node server but I would like a more permanent solution.

Please, any help?

SCRIPT 1:

description "node.js chat server"
author      "iandev ith3"

# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

script
    # Not sure why $HOME is needed, but we found that it is:
    export HOME="/root"

    exec /root/local/node/bin/node /home/ian/chat.js >> /var/log/node.log 2>&1
end script

post-start script
   # optionally put a script here that will notifiy you node has (re)started
   # /root/bin/hoptoad.sh "node.js has started!"
end script

SCRIPT 2:

description "node.js chat server"
author      "iandev ith3"

script
    exec /root/local/node/bin/node /home/ian/chat.js >> /var/log/node.log 2>&1
end script
Ian Herbert
  • 1,071
  • 2
  • 16
  • 35
  • I ended up just using the cron @reboot to make sure my script runs at startup since I was never able to get upstart or forever working on the server. It solves my overall goal but doesn't answer the question so I am leaving it as a comment. (in case people have similar problems but don't know about the cron @reboot) – Ian Herbert Oct 03 '12 at 15:56

3 Answers3

3

Just use Forever. https://github.com/indexzero/forever

JimBastard
  • 1,547
  • 1
  • 9
  • 2
  • I tried using forever, I installed with npm install forever and when issuing the 'forever' command, I just get this: 'bash: forever: command not found'. – Ian Herbert Sep 25 '11 at 02:27
  • Im trying with the second option on the forever github page, using an instance of Forever from inside the node.js code. Not sure how this works though I'm going to attempt to follow what it says on the page but any additional help is appreciated. – Ian Herbert Sep 25 '11 at 02:34
  • When I try to use forever from inside my node.js code, var forever = require('forever'); I get an error, Error: Cannot find module '../build/default/daemon' Any ideas? – Ian Herbert Sep 25 '11 at 02:37
  • SO I had to install forever with the -g option, now running 'forever' from the command line is recognized but I get the same error as above: Error: Cannot find module '../build/default/daemon' – Ian Herbert Sep 25 '11 at 02:49
  • You should read the documentation. If you have problems, you should use the issue tracker provider with the project. see: https://github.com/indexzero/forever – JimBastard Sep 25 '11 at 04:27
  • Thanks, someone over at the forever github page said to downgrade node to version 0.4.x and this problem will be fixed in node 0.6, so I went with node 0.4.11 and re-installed forever and still have the same errors. https://github.com/indexzero/forever/issues/122 – Ian Herbert Sep 25 '11 at 12:12
1

From looking at the website you provided I'd say that the /etc/init was just a typo and it should be /etc/init.d/. Some things you may want to check:

  • executable flag on your scripts. With most versions of Ubuntu executable files show up green when running 'ls' from the command line. If you want to check if your file is executable run 'ls -l /etc/init.d/YOUR_SCRIPT' from the command line. You will see something like this: -rwxr-xr-x 1 root root 1342 2010-09-16 10:13 YOUR_SCRIPT The x's mean that it is executable. To set the executable flag if it is not set, run chmod u+x YOUR_SCRIPT

  • I'm pretty sure for older versions of ubuntu you need to have the script in /etc/rc.d/rc3.d or /etc/rc3.d. What linux does is run through rc0.d to rc5.d and execute every script in there. From what it looks like, ubuntu is moving away from this to something simpler so if you have rc directories you may need to edit your script a little.

Anyway I think i'm getting a little over complicated here. Check your executable flag and if you have rc directories and we'll move on from there.

nick
  • 2,256
  • 2
  • 14
  • 14
  • I did make it executable with chmod u+x nodechat.conf, checking for the rc directories though. Thanks for the help. Yes, there is rc0.d through to rc6.d – Ian Herbert Sep 25 '11 at 02:52
  • I looked at the files in rc0.d, they seem like they are all symbolic links to files in /etc/init.d. Also, moving my script to rc0.d did not do anything different. – Ian Herbert Sep 25 '11 at 02:54
  • it sounds like you will want to put this in rc3.d (rc0.d is where linux starts running all the critical daemons such as mounting directories). So your script will go inside /etc/init.d and you will create a symbolic link to it inside rc3.d make sure you add SxxYOURSCRIPT (where xx is a number). What this does is tell linux when you want to start this daemon, higher numbers get done after lower ones and S stands for start. For now though just test to make sure your script is actually doing what you want it to. Then you can do all this stuff to start when it boots – nick Sep 25 '11 at 03:08
  • I did all this, left my script in init.d, created a symbolic link in rc3.d with the SXXSCRIPTNAME. If I try to run the upstart script by cd /etc/init.d then ./scriptname.conf, I get a whole lot of errors. If I try to issue this command: start SCRIPTNAME, I still get start: Unknown job: scriptname. – Ian Herbert Sep 25 '11 at 11:47
1

May not be the best thing to start a process with sudo, but here's what I have setup on my local pc:

#!upstart
description "node.js server"
author      "alessio"

start on startup
stop on shutdown

script
    export HOME="/ubuntu"

    exec sudo -u ubuntu /usr/bin/node /home/ubuntu/www/test.js 2>&1 >> /var/log/node.log
end script

Hope this helps.

alessioalex
  • 62,577
  • 16
  • 155
  • 122