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.

- 1,514
- 4
- 27
- 48
-
If you want to run it from cron, what's preventing you from doing that? Why do you think running it from ssh first will help? – Kenster Aug 13 '14 at 23:33
-
3A similar question was asked and answered here: http://stackoverflow.com/questions/29142/getting-ssh-to-execute-a-command-in-the-background-on-target-machine – vjones Aug 14 '14 at 00:09
-
You can use Lambda for cron, and SSM Run Command to remotely invoke scripts on EC2 instances. – jarmod Mar 21 '18 at 23:54
1 Answers
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.
- Attach Instance IAM role:
The ec2 instance must have IAM role with policy AmazonSSMFullAccess. This role enables the instance to communicate with the Systems Manager API.
- Install SSM Agent:
The EC2 instance must have SSM agent installed on it. The SSM Agent process the run command requests & configure the instance as per command.
- Execute command :
Example usage via AWS CLI:
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 services" --instance-ids "Instance-ID" --parameters commands="service --status-all" --region us-west-2 --output text

- 1,348
- 2
- 17
- 30