在ec2上远程运行命令 [英] Run a command remotely on ec2

查看:130
本文介绍了在ec2上远程运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ubuntu 14.04 ec2实例,上面有我的脚本。当我想在Mac终端上远程运行脚本时,我执行了 ssh myaws python MyFolder / myscript.py ,该脚本运行得很好。但是,即使关闭了Mac上的终端窗口或关闭了Mac,我仍希望脚本在ec2上运行。一旦弄清了这一点,我将继续使用cron来安排我的脚本在ec2上运行,而无需深思熟虑。这是可能的,我应该怎么做?现在,如果您需要更多说明,请让我。谢谢。

I have and Ubuntu 14.04 ec2 instance with my scripts uploaded there. When I want to run a script remotely using my mac's terminal I do ssh myaws "python MyFolder/myscript.py" which runs perfectly. However, I want the script to run on ec2 even after I close the terminal window on my mac or switch off my mac. Once I figure that out I will move onto using cron to schedule my scripts to run on ec2 without me ever ssh-ing in. Is it possible and if it is how would I go about it? Let me now if you need more clarification. Thanks.

推荐答案

是的,您可以使用AWS Systems Manager进行此操作。 AWS Systems Manager运行命令允许您在EC2以及本地服务器上远程安全地运行命令集。
以下是实现此目标的高级步骤。

Yes, you can do this with AWS Systems manager. AWS Systems Manager Run Command allows you to remotely and securely run set of commands on EC2 as well on-premise server. Below are high-level steps to achieve this.


  1. 附加实例IAM角色:

ec2实例必须具有策略为AmazonSSMFullAccess的IAM角色。该角色使实例能够与Systems Manager API进行通信。

The ec2 instance must have IAM role with policy AmazonSSMFullAccess. This role enables the instance to communicate with the Systems Manager API.


  1. 安装SSM代理:

EC2实例上必须安装了SSM代理。 SSM代理处理运行命令请求&按照命令配置实例。

The EC2 instance must have SSM agent installed on it. The SSM Agent process the run command requests & configure the instance as per command.


  1. 执行命令:

通过AWS CLI使用的示例:

Example usage via AWS CLI:

执行以下命令以检索在实例上运行的服务。将实例ID替换为ec2实例ID。

Execute the following command to retrieve the services running on the instance. Replace Instance-ID with ec2 instance id.

aws ssm send-command --document-name AWS-RunShellScript --comment listing服务 --instance-ids实例ID --parameters命令= service --status-all --region us-west-2-输出文本

更多详细信息:
https://www.justdocloud.com/2018/04/01/run-commands-remotely-ec2-instances/

这篇关于在ec2上远程运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆