简体   繁体   English

启动时AWS EC2实例未运行Java程序

[英]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. 我的EC2实例中有两个Java程序,它们是在公共子网中启动的。 The programs are stored under /home/ec2-user/559 folder as jar files named response_server.jar and server.jar. 程序以名为response_server.jar和server.jar的jar文件存储在/ home / ec2-user / 559文件夹下。 server.jar file is listening on port 5000 to talk to a client and response_server.jar is listening on port 6000 . server.jar文件在端口5000上侦听以与客户端通信,response_server.jar在端口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) ELB配置为对端口6000上的EC2实例执行ping操作以进行运行状况检查,因此,在端口6000上进行ping操作时,response_server.jar用于返回随机响应(主要用于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 . 第一个问题:当我进入EC2实例并使用以下命令手动运行jar文件时,它们按预期运行,并且我的客户端程序在端口5000和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. 但是,我希望这两个程序从启动EC2实例开始。 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. 除非我在ssh会话中手动运行它们,否则它不会连接。

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. 第二个问题:与第一个问题类似,除非我手动为ELB运行jar文件以检查端口6000的运行状况,否则我的ELB将此EC2实例显示为不正常。

I am unable to figure out why the java programs arent launching when rebooting the EC2 instance when using the launch script. 我无法弄清楚为什么使用启动脚本重新启动EC2实例时无法启动Java程序。 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. 您不必将jar文件放在用户日期中。 First you have to create a script. 首先,您必须创建一个脚本。 You can create a script for each of the jar files. 您可以为每个jar文件创建一个脚本。 For response_server.jar you can create script java_response_server_launch.sh like this: 对于response_server.jar,您可以创建脚本java_response_server_launch.sh,如下所示:

#! /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: 将您的脚本放在/etc/init.d目录下,然后使用以下命令:

update-rc.d java_response_server_launch.sh defaults

Also don't forget to chmod +x the script. 同样不要忘记使用chmod + x脚本。

More on this you can refer 对此您可以参考更多

Tool for creating a Java daemon service on Linux 在Linux上创建Java守护程序服务的工具

Running Commands on Your Linux Instance at Launch 启动时在Linux实例上运行命令

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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