WGET seems not to work with user data on AWS EC2 launch - bash

I launch an centos AMI I created, and try to add user data as a file which looks like this:
#!/bin/bash
mkdir /home/centos/testing
cd testing
wget https://validlink
So simply, on launch, the user data creates a folder called testing and downloads this validURL which I will not put as it links to my data - however it is valid and accessible.
When I launch the instance, the folder testing is created successfully, however there is no file inside the directory.
When I ssh into the instance, and run the wget command as a sudo, the file is downloaded successfully inside the testing folder.
Why does the file not get downloaded on the ec2 launch through user data?

You have no way of knowing the current working directory when you execute the cd command. So specify full path:
cd /home/centos/testing
Try this:
#!/bin/bash
mkdir /home/centos/testing
cd /home/centos/testing
wget https://validlink

Run it using the root user.
Try this instead:
#!/bin/bash
sudo su
yum -y install wget
mkdir /home/centos/testing
cd /home/centos/testing
wget https://validlink

Related

Ubuntu 18.04 - Add new user and initialize it

I'm creating a .sh bash script in Ubuntu 18.04.1 LTS where I need to create a new user using the "adduser" command:
sudo adduser newuser
After that, I need to perfomr other operations like add some files in the newuser's Desktop or Documents folders. The main problem is that, untill I don't reboot the system, the newuser haven't that folders so I need to create them manually
sudo mkdir -p /home/newuser/Desktop
sudo mkdir -p /home/newuser/Documents
sudo mkdir -p /home/newuser/.local/share/applications
I really don't like this solution. Is there a way to initialize that folders after a user creation?
Thanks
You can try using xdg-user-dirs-update tool which generates all required user directories in the $HOME path.
Don't forget to do su newuser first.

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.
You're calling a shell script within a shell script, so you need to make sure:
have this as the first line of your ec2 user data script #!/bin/bash
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.
Solution: CD into the directory first.
#!/bin/bash
cd /opt
wget https://example.com/myTAR.tar
tar -xvf myTAR.tar
/opt/mySH.sh

osx: How to install a package into user's application support directory?

I need to install 2 audio plugins to the root Audio/Plug-Ins/VST & Components directories. My installer does that fine. But I also need to install a directory of preset files into /Users/$USER/Library/Application Support/MyCompany folder.
I've heard that an installer can't install to / and ~ in the same installer, but I really want it to be 1 install for the user. So it seems like a good idea would be to install the VST and Components first. Then install the preset folder in a temporary location (like /tmp or similar) and then run a post-install script to move the files to the user's Library...but I can't get that to work.
This is the script I'm trying to run:
#!/bin/bash
# movePresets.sh
# I want something like this...but it doesn't work because $USER is root in the installer I believe
/usr/bin/sudo -u $USER mkdir -p "/Users/$USER/Library/Application Support/MyCompany/Presets"
/usr/bin/sudo -u $USER mv -r "/tmp/Presets" "$USER/$USER/Library/Application Support/MyCompany"
exit 0
Obviously, I don't know the proper way to access a user's directory as root. Help please...thank you.
Have you tried saving off the user in a variable first?
#!/bin/bash
realuser=$USER
# or
#realuser=$(whoami)
/usr/bin/sudo -u $realuser mkdir -p "/Users/$realuser/Library/Application Support/MyCompany/Presets"
/usr/bin/sudo -u $realuser mv -r "/tmp/Presets" "$realuser/$realuser/Library/Application Support/MyCompany"

run 'cordova prepare ios' from different folder

how can I run 'cordova prepare ios' from a different folder? Just perhaps from the parent folder of the app i try to build and prepare. Or from a shell script (but cd to folder doesn't work either within a shell script - console works perfectly!)
Build works with this:
/Users/someone/cordova/appname/platforms/ios/cordova/build
But there is no prepare inside the ios/cordova/ folder.
prepare is located #
/usr/local/lib/node_modules/cordova/bin/cordova prepare ios
How can I set a path? --path /Users/someone/cordova/appname/ doesn't work.
Or some other ideas?
Thanks!
****update****
This doesn't work (also pushd, popd):
ssh -l username -o 'HostKeyAlias example.com' example.com "(cd /Users/someone/cordova/appname && /usr/local/lib/node_modules/cordova/bin/cordova prepare ios)"
env: node: No such file or directory
But if I use just ls or pwd I'll get the correct path.
I tried also this:
ssh -l username -o 'HostKeyAlias example.com' example.com <<'ENDSSH'
cd /Users/someone/cordova/appname
cordova prepare
ENDSSH
****solution****
I already got it! (https://stackoverflow.com/a/1472444/2192501)
ssh -l username -o 'HostKeyAlias example.com' example.com "source /etc/profile;cd /Users/someone/cordova/appname;cordova prepare)"
the method to run commands from a different folder in a bash script is 'cd'
you can also look at pushd and popd http://en.wikipedia.org/wiki/Pushd_and_popd
another method to try would be
(cd path/to/cordova/project && cordova prepare ios)
this will spawn a subshell, change the working directory to the cordova project, execute the cordova prepare command, then close. This won't alter the working directory of the shell you execute from.
the prepare file which eventually gets executed is based on the platform arguments, and is downloaded off of your node_modules folder. It is called by the Cordova CLI with the correct arguments, including the path to the cordova project. The Cordova CLI knows where your cordova project is implicitly, because you executed the commands from the project root
I already got it! (https://stackoverflow.com/a/1472444/2192501)
ssh -l username -o 'HostKeyAlias example.com' example.com "source /etc/profile;cd /Users/someone/cordova/appname;cordova prepare)"

How to run a script file on Mac?

I have searched on how to run a script file on Mac but nothing works for me.
Every time I tried sudo script-name the terminal responds with
-bash: /Users/macuser/Desktop/tesseract-3.01: is a directory
The file I want to run is called start and it's located in tesseract-3.01 directory on the desktop.
simply do
/Users/macuser/Desktop/tesseract-3.01/start
or if it's actually called start.sh
/Users/macuser/Desktop/tesseract-3.01/start.sh
you might also want to do
chmod +x /Users/macuser/Desktop/tesseract-3.01/start.sh
to change the script to be executable before you run the script
sudo /Users/macuser/Desktop/tesseract-3.01/start
You have to indicate the script name, but it looks like you were only specifying the directory.
You could also cd to the directory and then run it like so:
cd /Users/macuser/Desktop/tesseract-3.01
sudo ./start
Try
sudo ./Users/macuser/Desktop/tesseract-3.01/start.sh
or
cd /Users/macuser/Desktop/tesseract-3.01
then
sudo ./start.sh

Resources