How to have a Play framework app autostart during boot on Elastic Beanstalk CentOS ec2 instances

So you’ve created an Elastic Beanstalk environment, you have a play framework distribution which you’ve created using play dist (either on your local environment, or right there on the server, whatever you prefer)

play dist outputs a my-app-1.0.zip file which has a self-contained version of your app with all the necessary libraries and a start script.

Afer you unzip it, you end up with a my-app-1.0/lib/ folder and a start script.

[pastacode lang=”bash” manual=”%5Bec2-user%40ip-10-235-8-106%20bullq-1.0%5D%24%20ls%20-l%0Atotal%2024%0Adrwxrwxr-x%202%20ec2-user%20ec2-user%204096%20Sep%2027%2015%3A35%20lib%0A-rwxrwxr-x%201%20ec2-user%20ec2-user%204328%20Sep%2027%2015%3A35%20start” message=”” highlight=”” provider=”manual”/]

Make sure it’s executable by using chmod +x start on the start script.

So now, this is all in the first ec2 instance of your elastic beanstalk environment, if you’re like me and you’ve used ubuntu/debian for your server management things can be slightly different here, since Amazon preferred CentOS for their default image, and here I’ll show you how to make your play app auto start when the server boots because you want every new machine that may be instanciated to have your app installed and to start the service as soon as the machine is up.

Create a /etc/init.d/myappd script
(I’m using ‘myapp’ here as an example, your app can be named whatever is named, so replace accordingly)

[pastacode lang=”bash” manual=”%23!%2Fusr%2Fbin%2Fenv%20bash%0A%23myappd%0A%23Script%20to%20start%7Cstop%7Crestart%20myappd%20from%20%2Fetc%2Finit.d%2F%0A%23By%20Gubatron%20%E2%80%93%20%40gubatron%20%E2%80%93%20gubatron%40gmail.com%0A%0A%23replace%20accordingly%20in%20these%20variables%20%E2%80%98myapp%E2%80%99%20for%20the%20name%20of%20your%20app%0APID_FILE%3D%2Fhome%2Fec2-user%2Fmyapp%2Fdist%2Fmyapp-1.0%2FRUNNING_PID%0ADAEMON_NAME%3Dmyappd%0ADAEMON_PATH%3D%2Fhome%2Fec2-user%2Fmyapp%0ADAEMON%3D%24DAEMON_PATH%2Fdist%2Fmyapp-1.0%2Fstart%0A%0Atest%20-x%20%24DAEMON%20%7C%7C%20exit%200%0A%0Aset%20-e%0A%0Afunction%20killDAEMON()%20%7B%0Aecho%20%E2%80%9Cstart%20kill%20daemon%E2%80%9D%0Akill%20-9%20cat%20%2Fhome%2Fec2-user%2Fbullq%2Fdist%2Fbullq-1.0%2FRUNNING_PID%0Aecho%20%E2%80%9Cend%20kill%20daemon%E2%80%9D%0A%7D%0A%0Afunction%20removePIDFile()%20%7B%0Aif%20%5B%20-e%20%24PID_FILE%20%5D%0Athen%0Arm%20-f%20%24PID_FILE%0Afi%0A%7D%0A%0Acase%20%241%20in%0Astart)%0AremovePIDFile%0Aecho%20%E2%80%9CStarting%20%24DAEMON_NAME%E2%80%A6%20%24DAEMON%E2%80%9D%0Anohup%20%24DAEMON%20%26%0A%3B%3B%0Arestart)%0Aecho%20%E2%80%9CHot%20restart%20of%20%24DAEMON_NAME%E2%80%9D%0AkillDAEMON%0AremovePIDFile%0ACOMMAND%3D%E2%80%9Dnohup%20%24DAEMON%20%26%E2%80%9D%3B%0Aecho%20%24COMMAND%0A%24COMMAND%0Arm%20-f%20%24PID_FILE%0A%3B%3B%0Astop)%0Aecho%20%E2%80%9CStopping%20%24DAEMON_NAME%E2%80%9D%0AkillDAEMON%0AremovePIDFile%0A%3B%3B%0A*)%0Aecho%20%E2%80%9CUsage%3A%20%24DAEMON_NAME%20%7Bstart%7Crestart%7Cstop%7D%E2%80%9D%20%3E%262%0Aexit%201%0A%3B%3B%0Aesac%0A%0Aexit%200″ message=”” highlight=”” provider=”manual”/]

 

Wire it to autostart

The simplest way I found to have this script start when the server would boot was to add it at the end of the
/etc/rc.local file. (In ubuntu you’d register the new script with the upate-rc.d command)

[pastacode lang=”bash” manual=”%23!%2Fbin%2Fsh%0A%23%0AThis%20script%20will%20be%20executed%20after%20all%20the%20other%20init%20scripts.%0AYou%20can%20put%20your%20own%20initialization%20stuff%20in%20here%20if%20you%20don%E2%80%99t%0Awant%20to%20do%20the%20full%20Sys%20V%20style%20init%20stuff.%0A%0Atouch%20%2Fvar%2Flock%2Fsubsys%2Flocal%0A%0A%2Fetc%2Finit.d%2Fmyappd%20start” message=”” highlight=”” provider=”manual”/]

 

4 thoughts on “How to have a Play framework app autostart during boot on Elastic Beanstalk CentOS ec2 instances

  1. What AMI are you using to deploy playframework on elastic beanstalk? Did you do a custom AMI and if so what is it based on?

    1. I believe I used an official AMI by Amazon. Wish I could check what I used, but the project was cancelled and the services were removed.

  2. Hi, Im following your tutorial, and only this command works /etc/init.d/myappd start

    stop and restart don work, in the terminal they output:
    [ec2-user@ip-10-244-167-104 ~]$ /etc/init.d/myappd restart
    Hot restart of myappd
    start kill daemon
    cat: /home/ec2-user/hhsj-1.0-SNAPSHOT/RUNNING_PID: No such file or directory
    kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec … or kill -l [sigspec]

  3. I know this is an old post, but relevant to what I am trying to do (same thing with Play 2.3.7 and EC2). Do you choose to build your environment, deploy your play application through the “play dist or activator dist” to the environment, then create/register the shell script above, and then create an Image and configure that Image in the Autoscale? Does that mean that everytime you do a new deployment of your app, you create a new Image that is put into the Autoscale config?

Leave a Reply

Your email address will not be published. Required fields are marked *