[英]How do I make my server application run on an EC2 instance?
I developed a Java server (using Spring) and uploaded the final executable JAR to an EC2 instance using FileZilla.我开发了一个 Java 服务器(使用 Spring)并使用 FileZilla 将最终的可执行文件 JAR 上传到 EC2 实例。 Now I want it to run.现在我想让它运行。
I've connected via SSH and used java -jar server.jar
to run my server, and it worked (I've tried accessing it).我已经通过 SSH 连接并使用java -jar server.jar
来运行我的服务器,并且它有效(我已经尝试访问它)。 However once the SSH connection is closed the server obviously stops running as well.然而,一旦 SSH 连接关闭,服务器显然也会停止运行。
How can I start my application in such a way so it keeps running?我怎样才能以这种方式启动我的应用程序以使其保持运行?
Edit: Using the command screen
explained here I was able to run it in background and so it keeps running.编辑:使用此处解释的命令screen
,我能够在后台运行它,因此它一直在运行。
The issue is not cloud dependent its the configuration you have to do to run your jar as a service in your system. 问题不在于云,它取决于在系统中将jar作为服务运行所需的配置。 If you are using Elastic Bean Stalk change systemctl to initctl in below example. 如果您使用的是Elastic Bean Stalk ,请在以下示例中将systemctl更改为initctl 。
/usr/lib/systemd/system/demo.service /usr/lib/systemd/system/demo.service
[Unit]
Description=Demo Script
[Service]
Type=forking
ExecStart=/usr/bin/demoscript.sh
You need to make it run as daemon process in linux. 您需要使其在Linux中作为守护进程运行。
There are many tutorial / templates available to create a daemon shell script. 有许多教程/模板可用于创建守护程序shell脚本。 Quick google search shows github has many templates, so check them out. 快速的Google搜索显示github有很多模板,因此请检查一下。
You could try using systemd which is a Linux service manager.您可以尝试使用 systemd,它是一个 Linux 服务管理器。 You can use it to run your service in the background.您可以使用它在后台运行您的服务。
To do that you need to first create a unit file
that describes how systemd should manage your service (more info here ).为此,您需要首先创建一个描述 systemd 应如何管理您的服务的unit file
(更多信息,请点击此处)。
sudo vim /etc/systemd/system/your-application.service
Your file might look something like this您的文件可能看起来像这样
[Unit]
Description=Java Application as a Service
[Service]
User=ec2-user
#change this directory into your workspace
#mkdir workspace
WorkingDirectory=/home/ec2-user/workspace
#path to the executable bash script which executes the jar file
ExecStart=/bin/bash /home/ec2-user/workspace/your-script.sh
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Then in your home directory /home/ec2-user/workspace
you can create the bash script that will run your java application.然后在您的主目录/home/ec2-user/workspace
中,您可以创建 bash 脚本来运行您的 java 应用程序。
sudo nano your-script.sh
Your script might look like this您的脚本可能如下所示
#!/bin/sh
java -jar your-application.jar
All you need to do then is start the service with the command然后您需要做的就是使用命令启动服务
sudo systemctl enable your-application.service
sudo systemctl start your-application.service
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.