1

I have a script on my local machine which helps me to connect to my ec2. But, it does not runs the script file specified.

awsconnect.sh:

ssh -i ".pemfile" ubuntu@"ec2-instance"


  ./data.sh

data.sh is my file on the aws-ec2.

data.sh:

 mkdir -p dumps/$(date +"%Y%m%d");
    mysqldump -h localhost -port=3306 -u root -proot abc | gzip > dumps/$(date +"%Y%m%d")/abc.sql.gz;
    logout

My data.sh file is running fine if i run it from aws-ec2 command line. But, it is not running from my script file. What is the problem?

DarkCygnus
  • 7,420
  • 4
  • 36
  • 59
Aman Srivastava
  • 83
  • 1
  • 10
  • So using this file "awsconnect.sh" you connect to instance and how do you run the "data.sh" file from your script? – Piyush Patil Jun 10 '16 at 15:24
  • 1
    The script is waiting for the `ssh` command to finish and exit, and then it will run the next command, locally. That's obviously not what you want. You have to pass the remote command you want *into* the `ssh` command. See drewyupdrew's answer for the solution. – Mark B Jun 10 '16 at 15:38

1 Answers1

7

Are you able to ssh into the machine fine? If so, then you just need to make sure that ownership and permissions are ok for the script. Then you can:

ssh -i key.pem ubuntu@ec2-instance "bash /path/to/your/script/data.sh"

However, if things in your script need root access, then you would need permissions.

Edit: As @error2007s mentioned, I forgot to specify the identity file in my command. I've edited the command, so put that in awsconnect.sh and it should work fine.

drewyupdrew
  • 1,549
  • 1
  • 11
  • 16
  • @AmanSrivastava wait, after you ran that command I posted, it logged you into the machine? That shouldn't happen, the command I posted should run the script on the machine, not log you in. Can you post more information, do you get an error message? Post as much info as you can, i.e. paste your whole command/command line output in your post. Lots of people here on SO willing to help out, but it's hard to help when all we know is "it's not working" :) – drewyupdrew Jun 10 '16 at 16:15
  • @drewyupdrew I have a script file on my instance with the name data.sh which is executable. The script for my data.sh is given above. It works fine when I run it from that instance and create dumps of my database. But when I ran your command from my machine it gave me the following error - "Permission denied (publickey)" And the script on that machine doesn't runs. Thanks for your help! :) – Aman Srivastava Jun 10 '16 at 16:41
  • @drewyupdrew Your answer states to run that command from the Ec2 instance user wants to run the command from his local machine. – Piyush Patil Jun 10 '16 at 16:47
  • 1
    @AmanSrivastava Please add this command in your awsconnect.sh "ssh -i key.pem ubuntu@ec2-instance "bash /path/to/your/script/data.sh"" – Piyush Patil Jun 10 '16 at 16:54
  • Yes, may need to add the identity file as @error2007s mentioned, if the default key file is incorrect. (@error2007s, you may want to use backticks around the whole command rather than double quotes to avoid confusion) – drewyupdrew Jun 10 '16 at 17:01
  • @error2007s why does my answer state to run the command from the EC2 instance? That command is definitely to be run on his **local machine** since I'm ssh'ing into the EC2 instance. I don't understand what you mean. – drewyupdrew Jun 10 '16 at 17:04
  • @drewyupdrew Because you said "Are you able to ssh into the machine fine?" which user already stated he is able to do that using "awsconnect.sh" only thing happening is data.sh is not running when he runs "awsconnect.sh" on his local machine. User is asking what changes he needs to do his script file "awsconnect.sh" and not what command he needs to run from his EC2 instance or local command line. – Piyush Patil Jun 10 '16 at 17:11
  • @drewyupdrew That command will still fail because the the key file needs to be specified to ssh to the EC2 instance your command is missing that – Piyush Patil Jun 10 '16 at 17:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114377/discussion-between-drewyupdrew-and-error2007s). – drewyupdrew Jun 10 '16 at 17:22
  • Hey thanks @error2007s your command works fine! :) I was trying to do the same but my mistake was that i was logging into the remote terminal first and then trying to run the command which is not the correct way. – Aman Srivastava Jun 13 '16 at 06:45
  • @AmanSrivastava cool, glad you got it to work! I've edited my answer and added the identity file. Keep in mind, sometimes you don't need to specify your private key since the `ssh` command uses a default file depending on your ssh version (protocol) i.e. `~/.ssh/identity` or `~/.ssh/id_rsa`. In your case, your private key was different than the default so you need to specify it. – drewyupdrew Jun 13 '16 at 14:36