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?
Use Elastic Beanstalk :) Provides support for auto-scaling, SSL termination, blue/green deployments, etc
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):
For Ubuntu:
2) Create upstart script for your node app:
in
/etc/init
add fileyourappname.conf
with the following lines of code:3) start your app by
sudo start yourappname
You can create a script that can start and stop your app and place it in /etc/init.d; make the script adhere to chkconfig's conventions (below), and then use chkconfig to set it to start when other services are started.
You can pick an existing script from /etc/init.d to use as an example; this article describes the requirements, which are basically:
Once your script is set up, you can use
and you should be good to go. (Some distros may require you to manually symlink to the script to /etc/init.d/rc.d, but I believe your AWS distro will do that for you when you enable the script.
My Amazon Linux instance runs on Ubuntu, and I used systemd to set it up.
First you need to create a service file. (in my case cloudyleela.service)
Type the following in this file:
Reload the file from disk, and then you can start your service. You need to enable it to make it active as a service, which automatically launches at startup.
A great systemd for node.js tutorial is available here.
If you run a webserver:
You probably will have some issues running your webserver on port 80. And the easiest solution, is actually to run your webserver on a different port (e.g. 4200) and then to redirect that port to port 80. You can accomplish this with the following command:
Unfortunately, this is not persistent, so you have to repeat it whenever your server restarts. A better approach is to also include this command in our service script:
ExecStartPre
to add the port forwardingExecStopPost
to remove the port forwardingPermissionStartOnly
to do this with sudo powerSo, something like this:
Don't forget to reload and restart your service:
Have been using forever on AWS and it does a good job. Install using
To add an application use
and to stop the application use
This is a useful article that helped me with setting it up.
You can use
screen
. Runcrontab -e
and add this line: