HolaMan
HolaMan

Reputation: 391

how to restart node application automatically on aws elastic-beanstalk

I have googled this question for a while but can't find the answer. My question is while deploying the nodejs application on aws elastic-beanstalk servers, I want the nodejs application would be restart automatically if the application crash.

Actually there're few nodejs package already support this by command line, such as forever, but there's no easy way from console management or awscli to install this package and execute it to achieve restart automatically.

I am wondering how do you resolve the restart issue on aws eb?

Upvotes: 22

Views: 17994

Answers (6)

user566245
user566245

Reputation: 4227

Yes it does. I know because after ssh'ing in to the box i did:

sudo pkill node

And then I can verify that:

  1. the app continues to work from the browser
  2. It prints restarting to logs visible via: sudo tail /var/log/web.stdout.log -f
  3. A new node process with new process id takes its place visible via: pgrep node

Upvotes: 2

Jeremy
Jeremy

Reputation: 1835

After playing around with this a bit, and inspecting the process immediately after running

aws elasticbeanstalk restart-app-server --environment-name my-env

from @Human Love 's comment. I found these two commands for manually starting/stopping the process when ssh'd into the EC2. Not sure if these are recommended, but for quick debugging I find them useful

# to start the process
python /opt/elasticbeanstalk/containerfiles/ebnode.py --action start-all
# to stop the process
sudo python /opt/elasticbeanstalk/containerfiles/ebnode.py --action stop-all

[NOTE]: this is a nodejs specific solution. Though other application types are probably pretty similar. To inspect the exact command. Open two terminal windows and

  1. in the first, run aws elasticbeanstalk restart-app-server --environment-name my-env
  2. in the second, run ps aux | grep python (I grepped for node because it was a node app)

to find the specific /opt/elasticbeanstalk script

Upvotes: 2

Ali Nouman
Ali Nouman

Reputation: 3414

If you want to restart the server from cron then you could use these commands.

aws elasticbeanstalk restart-app-server --environment-name my-env

Reference

Upvotes: 1

ryanman
ryanman

Reputation: 3834

I've confirmed (as of Mar 11, 2015) that EB does indeed restart node for you.

To confirm, I added a hidden query param to my app:

if (req.query.testcrash == 'true') {
    setTimeout(function(){
        undefined.crashMe();
    }, 100);
}

Then verified in the log that the exception happened, and that my app was restarted.

For reference:

  • My EB/EC2 config is "64bit Amazon Linux 2014.09 v1.0.9 running Node.js"
  • Using nginx and node 0.10.31

Upvotes: 34

davychiu
davychiu

Reputation: 101

Add forever to your package.json so it gets installed automatically. Then in EB console, under configuration, custom node command:

node_modules/.bin/forever app.js

Upvotes: 9

Vadym Fedorov
Vadym Fedorov

Reputation: 2415

Yes, better option to use Supervisor, however in order to have ability to restart app server with help of aws console or beanstalk cli tools you need to put own handler to Elastic beanstalk hooks in the directory: /opt/elasticbeanstalk/hooks/restartappserver/enact Hook is shell, python or ruby script that placed in mentioned directory. Put logic of the supervisord restart here and you will be able to restart it with help of management console, aws cli tools (http://docs.aws.amazon.com/cli/latest/reference/elasticbeanstalk/restart-app-server.html), elastic beanstalk api: (http://docs.aws.amazon.com/elasticbeanstalk/latest/APIReference/API_RestartAppServer.html)

How to add hook, install supervisiord etc you can read here: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html

Upvotes: 3

Related Questions