Java 如何在 Amazon EC2 中运行 Jar?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21314700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:00:01  来源:igfitidea点击:

How to run a Jar in Amazon EC2?

javaeclipseamazon-web-servicesamazon-ec2cloud

提问by Lemon Juice

I am fairly new to Amazon. I have a Java file which reads GBs of crawled data and I am running this using AWS ToolKit for Eclipse. The disadvantage here is, I have to keep my machine running for weeks if I need to read the entire crawled data and that is not possible. Apart from that, I can't download GBs of data in to my local PC (Because it is reading data).

我对亚马逊相当陌生。我有一个 Java 文件,它读取 GB 的爬网数据,我正在使用AWS ToolKit for Eclipse运行它。这里的缺点是,如果我需要读取整个爬网数据,我必须让我的机器运行数周,而这是不可能的。除此之外,我无法将 GB 的数据下载到我的本地 PC(因为它正在读取数据)。

Is there any way that I can upload the Jar to Amazon, and Amazon run it without engaging with my computer? I have heard about web crawlers running in Amazon for weeks without downloading data into the developers machine, and without letting the developer to turn on his machine without shutting down for months.

有什么方法可以让我将 Jar 上传到亚马逊,然后亚马逊在不使用我的计算机的情况下运行它?我听说网络爬虫在亚马逊上运行了数周而没有将数据下载到开发人员的机器中,也没有让开发人员打开他的机器而不关闭数月。

The feature I am asking is just like "job flows" in Amazon Elastic Map-Reduce. You upload the code, it runs it inside. It doesn't matter whether you keep "your" machine turned on or not.

我要问的功能就像 Amazon Elastic Map-Reduce 中的“工作流”。你上传代码,它在里面运行它。您是否让“您的”机器保持开启状态并不重要。

采纳答案by Al Sweetman

You can run with the nohupcommand for *nix

您可以使用*nix的nohup命令运行

nohup java -jar myjar.jar 2>&1 >> logfile.log &

This will run your jar file, directing the output [stderr and stdout] to logfile.log. The &is needed so that it runs in the background, freeing up the command line / shell/

这将运行您的 jar 文件,将输出 [stderr 和 stdout] 定向到logfile.log. 的&需要,以便它在后台运行,从而释放了命令行/壳/

!! EDIT !!

!! 编辑 !!

It's worth noting that the easiest way I've found for stopping the job once it's started is:

值得注意的是,我发现在工作开始后停止工作的最简单方法是:

ps -ef | grep java

ps -ef | grep java

Returns ec2-user 19082 19056 98 18:12 pts/0 00:00:11 java -jar myjar.jar

退货 ec2-user 19082 19056 98 18:12 pts/0 00:00:11 java -jar myjar.jar

Then kill 19082.

然后kill 19082

Note, you can tail -f logfile.logor other such derivatives [less, cat, head] to view the output from the jar.

请注意,您可以tail -f logfile.log或其他此类派生 [less, cat, head] 来查看 jar 的输出。

Answer to question/comment

回答问题/评论

Hi. You can use System.out.println(), yes, and that'll end up in logfile.log. The command that indicates that is the 2&>1which means "redirect stream 2 into stream 1". In unix speak that means redirect/pipe stderr into stdout. We then specify >> logfile.logwhich means "append output to logfile.log". As System.out.println() writes to stdout it'll end up in logfile.log.

你好。您可以使用System.out.println(), 是的,这将在 logfile.log 中结束。指示 的命令是2&>1“将流 2 重定向到流 1”。在 unix 中,这意味着将 stderr 重定向/管道到 stdout。然后我们指定>> logfile.log这意味着“将输出附加到 logfile.log”。当 System.out.println() 写入标准输出时,它将最终出现在 logfile.log 中。

However, if you're app is set up to use Log4j/commons-logging then using LOG.info("statement");will end up in the configured 'log4j.properties' log file. With this configuration the only statements that will end up in logfile.logwill be those that are either System generated (errors, linux internal system messages) or anything that's written explicitly to the stdout (ie System.out.println()) statements;

但是,如果您的应用程序设置为使用 Log4j/commons-logging,则 usingLOG.info("statement");将最终出现在配置的“log4j.properties”日志文件中。使用此配置,最终logfile.log会出现的唯一语句将是那些系统生成的(错误、linux 内部系统消息)或任何明确写入 stdout(即System.out.println())语句的语句;