Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I automatically start a node.js application in Amazon Linux AMI on aws?

Is there a brief guide to explain how to start up a application when the instance starts up and running? If it were one of the services installed through yum then I guess I can use /sbin/chkconfig to add it to the service. (To make it sure, is it correct?)

However, I just want to run the program which was not installed through yum. To run node.js program, I will have to run script sudo node app.js at home directory whenever the system boots up.

I am not used to Amazon Linux AMI so I am having little trouble finding a 'right' way to run some script automatically on every boot.

Is there an elegant way to do this?

like image 211
user482594 Avatar asked Jun 30 '12 16:06

user482594


3 Answers

One way is to create an upstart job. That way your app will start once Linux loads, will restart automatically if it crashes, and you can start / stop / restart it by sudo start yourapp / sudo stop yourapp / sudo restart yourapp.

Here are beginning steps:

1) Install upstart utility (may be pre-installed if you use a standard Amazon Linux AMI):

sudo yum install upstart 

For Ubuntu:

sudo apt-get install upstart 

2) Create upstart script for your node app:

in /etc/init add file yourappname.conf with the following lines of code:

#!upstart description "your app name"  start on started mountall stop on shutdown  # Automatically Respawn: respawn respawn limit 99 5  env NODE_ENV=development  # Warning: this runs node as root user, which is a security risk # in many scenarios, but upstart-ing a process as a non-root user # is outside the scope of this question exec node /path_to_your_app/app.js >> /var/log/yourappname.log 2>&1 

3) start your app by sudo start yourappname

like image 141
mvbl fst Avatar answered Sep 23 '22 21:09

mvbl fst


You can use forever-service for provisioning node script as a service and automatically starting during boots. Following commands will do the needful,

npm install -g forever-service forever-service install test 

This will provision app.js in the current directory as a service via forever. The service will automatically restart every time system is restarted. Also when stopped it will attempt a graceful stop. This script provisions the logrotate script as well.

Github url: https://github.com/zapty/forever-service

As of now forever-service supports Amazon Linux, CentOS, Redhat support for other Linux distro, Mac and Windows are in works..

NOTE: I am the author of forever-service.

like image 42
arva Avatar answered Sep 19 '22 21:09

arva


Quick solution for you would be to start your app from /etc/rc.local ; just add your command there.

But if you want to go the elegant way, you'll have to package your application in a rpm file, have a startup script that goes in /etc/rc.d so that you can use chkconfig on your app, then install the rpm on your instance.

Maybe this or this help. (or just google for "creating rpm packages")

like image 33
Unknown Avatar answered Sep 19 '22 21:09

Unknown