简体   繁体   English

如何在 EC2 上远程执行带有命令行参数的程序(java 程序)?

[英]How to execute a program with command line arguments on EC2 remotely (java program)?

I want to start a EC2 instance from java code and execute python code on that EC2 instance.我想从 java 代码启动一个 EC2 实例并在该 EC2 实例上执行 python 代码。

  1. Create a custom AMI with python console application使用 python 控制台应用程序创建自定义 AMI
  2. launch EC2 instance using AMI from java application从 Java 应用程序使用 AMI 启动 EC2 实例
  3. launch multiple instances of python program with different command line arguments from java code.使用来自 java 代码的不同命令行参数启动 python 程序的多个实例。

I have done some analysis where I can create AMI with python code and launch EC2 instance, but I am looking for a way to launch multiple instances of python program with command line arguments.我已经做了一些分析,我可以用 python 代码创建 AMI 并启动 EC2 实例,但我正在寻找一种方法来启动带有命令行参数的 python 程序的多个实例。

在此处输入图片说明

Basically I want to build a scalable system which will start EC2 instance and launch multiple python processes as per requirement and will remove EC2 instance as work is done.基本上我想构建一个可扩展的系统,它将启动 EC2 实例并根据需要启动多个 python 进程,并在工作完成时删除 EC2 实例。

UPDATE: Although accepted answer is correct there is another way to execute scripts remotely using ssh tunneling using JSCH java library , here is the Gist for sample code更新:虽然接受的答案是正确的,但还有另一种使用 JSCH java 库使用 ssh 隧道远程执行脚本的方法,这里是示例代码的要点

This can be used for communication with non AWS machine with SSH service on.这可用于与开启 SSH 服务的非 AWS 机器进行通信。

You can use AWS Systems Manager to invoke shell commands on your remote EC2 instance.您可以使用 AWS Systems Manager 在远程 EC2 实例上调用 shell 命令。

  1. Create a document in AWS SSM which invokes the shell commands.在 AWS SSM 中创建一个调用 shell 命令的文档。
  2. Create an EC2 role which allows SSM to run commands on EC2 instance on your behalf.创建一个 EC2 角色,允许 SSM 代表您在 EC2 实例上运行命令。
  3. Attach that role to that instance and wait for that instance to come under Managed Instance inside SSM.将该角色附加到该实例并等待该实例进入 SSM 内的Managed Instance
  4. Run that SSM document on that instance from Java.从 Java 在该实例上运行该 SSM 文档。

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

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