While launching AWS EC2 instance the java program is not executing what might be the issue

+1 vote

I have two Java programs in my EC2 instance which is launched in a public subnet. The programs are stored under /home/ec2-user/559 folder as jar files named response_server.jar and server.jar. server.jar file is listening on port 5000 to talk to a client and response_server.jar is listening on port 6000 . ELB is configured to ping the EC2 instances on port 6000 for health checking and hence response_server.jar is used to return a random response when pinged on port 6000 (mainly for ELB)

I am having two issues :

First issue : When I ssh in to the EC2 instance and manually run the jar files using the command below, they work as expected and my client program receives the response on both port 5000 and 6000 .

cd /home/ec2-user/559/
java -jar response_server.jar & java -jar server.jar

However, I want the two programs to start with the launch of the EC2 instance. Hence I added them as a part of my user-data :

#! /bin/bash
java -jar /home/ec2-user/559/response_server.jar & 
java -jar /home/ec2-user/559/server.jar &

Now, if I connect the client program, I get an error that the connection is refused. Unless I manually run them in the ssh session, it doesn't connect.

Second issue : Kind of related to the first issue, my ELB shows this EC2 instance as unhealthy unless I manually run the jar files for ELB to check the health status on port 6000.

I am unable to figure out why the java programs arent launching when rebooting the EC2 instance when using the launch script. I checked to see if the ports are open after booting the instance and find that the ports are not in listening state.

Any help would be of great help !

May 13, 2019 in AWS by code_ninja
• 6,300 points

retagged May 16, 2019 by Kalgi 3,485 views

1 answer to this question.

+1 vote
  • Well you are facing a script issue
  • So first create a separate script for each jar file
  • You don't have to put jar file in your user-date, instead for refrence use below snipet for response_server.jar to create java_response_server_launch.sh script
#! /usr/bin/sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
JAVA=/usr/bin/java
MY_SERVER=/home/ec2-user/559/response_server.jar & 
USER=your_username
/bin/su - $USER -c "$JAVA -jar $MY_SERVER &"
  • Put your script under /etc/init.d directory, and then use the command: update-rc.d java_response_server_launch.sh defaults
  • later use chmod +x the script, to change the access mode of the file. 

Hope this helps!

Check out Java certification course to become certified expert.

Thanks!

answered May 13, 2019 by ArchanaNagur
• 2,360 points

Related Questions In AWS

0 votes
1 answer
+1 vote
1 answer

What AWS EC2 instance is more suitable for Chat Application?

I would recommend starting with a T2.micro ...READ MORE

answered Jul 10, 2018 in AWS by Priyaj
• 58,020 points
964 views
0 votes
2 answers

Launching an EC2 Instance using AWS SDK Java

package com.ec2application.ec2application; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.regions.Regions; import com.amazonaws.services.ec2.AmazonEC2; import ...READ MORE

answered Jan 22, 2019 in AWS by Priyaj
• 58,020 points
6,883 views
0 votes
1 answer

How to Pass the VPC ID while creating the Ec2 instance in AWS using Python Boto3

import boto3 ec2 = boto3.resource('ec2') instance = ec2.create_instances( ...READ MORE

answered Jan 29, 2019 in AWS by Priyaj
• 58,020 points
3,548 views
+1 vote
3 answers
0 votes
2 answers
+1 vote
1 answer

What is the difference between an Instance, AMI and Snaphots in AWS?

AMI is the Amazon Machine Image which provides ...READ MORE

answered May 24, 2019 in AWS by ArchanaNagur
• 2,360 points
4,215 views
0 votes
1 answer

What is the future of AWS?

Amazon Web Services (AWS) is a secure ...READ MORE

answered May 31, 2019 in AWS by ArchanaNagur
• 2,360 points
2,303 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP