简体   繁体   中英

AWS EC2 instance not running Java program when launching

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 !

You don't have to put jar file in your user-date. First you have to create a script. You can create a script for each of the jar files. For response_server.jar you can create script java_response_server_launch.sh like this:

#! /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

Also don't forget to chmod +x the script.

More on this you can refer

Tool for creating a Java daemon service on Linux

Running Commands on Your Linux Instance at Launch

让AWS Elastic Beanstalk使用Java SE平台为您做到这一点: https : //docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-se-platform.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM