Jim Taylor
Jim Taylor

Reputation: 121

EC2 User Data Script .sh file and Manual Execute Differ

I am trying to execute the following user data script

sudo wget https://files.mysite.com/downloads/myFile.tar -P /opt
sudo tar -xvf /opt/myTar.tar -C /opt
sudo /opt/myFile.sh

When I execute the .sh file manually I see this:

Extracting...
Unpacking...
Cleaning up...
Complete

and it creates a directory in /opt/myDirectory

I do see the console in /var/log/cloud-init-output.sh but it doesn't seem to create the directory when run as part of the userdata script.

Upvotes: 0

Views: 7604

Answers (2)

Jim Taylor
Jim Taylor

Reputation: 121

Solution: CD into the directory first.

#!/bin/bash
cd /opt
wget https://example.com/myTAR.tar
tar -xvf myTAR.tar
/opt/mySH.sh

Upvotes: 1

Frederic Henri
Frederic Henri

Reputation: 53783

You're calling a shell script within a shell script, so you need to make sure:

  1. have this as the first line of your ec2 user data script #!/bin/bash

  2. call myFile.sh with the source command (alias is .) like this: . /opt/myFile.sh so it will run the myFile script

Note: the ec2 user data script runs as root so you do not need to have sudo each time you run a command.

Upvotes: 3

Related Questions