Bash: `chmod a+x` Not Working When Run Inside Script - bash

I'm trying to simulate an update process via script.
I only care about the update script's exit code (0 for success and any other value for failure).
I have created a simple script called update.sh to simulate an update:
#!/bin/bash
# 1=true, 0=false
success=1
if [ $success -eq 1 ] ; then
# Success.
exit 0
else
# Failure.
exit 1
fi
To simulate a downloaded update, I have zipped update.sh into a file called update-file.zip.
My main script extracts update-file.zip and runs update.sh:
#!/bin/bash
# Create a fresh update folder.
rm -rfv /test/update && mkdir /test/update
# Simulate download by copying zip file to that folder.
cp -rf /update-file.zip /test/update/
cd /test/update
unzip -o update-file.zip
# Make the update script executable.
updateFile="/test/update/update.sh"
chmod a+x $updateFile
# Run the update.
/test/update/update.sh
if [ $? -ne 0 ] ; then
echo "update failed"
else
echo "update success"
fi
# Delete update folder.
rm -rfv /test/update
However, when I run my main script (even with sudo), I get the following error message:
/test/update/update.sh: Permission denied
Not even using a service (that uses root to do everything) helped, as I still got the same error message.
It seems that chmod a+x is not working when run inside scripts, because if I run chmod a+x /test/update/update.sh on the terminal, everything works fine.
Whenever chmod a+x is run from a script, I get "Permission denied" errors when trying to run the affected script.
What puzzles me is that when I run ls -l /test/update/update.sh, I get the following:
-rwxrwxrwx 1 root root 131 Sep 9 2020 /test/update/update.sh
The output is the same regardless of whether chmod a+x is run from the terminal or a script.
I am on Ubuntu Server 20.04.1 LTS (Focal Fossa).

You may explicitly run the interpreter for the file:
bash /test/update/update.sh
There is no need to make the file executable then.

Could you please use
/bin/bash /test/update/update.sh
instead of
/test/update/update.sh
in script.
Could you please provide output of below command as well once.
ls -ld /test/update

Related

Shell script error: How would I change a permission on a file in a Shell script

I am trying to set up a cron on several AWS EC2 machines and would like to run a command on all of them at once, with the following shell script:
#!/bin/sh
cd /etc/cron.daily
touch ecs.sh
echo '#!/bin/sh' > /etc/cron.daily/ecs.sh
echo 'sudo yum update -y ecs-init' >> /etc/cron.daily/ecs.sh
echo 'sudo yum update -y' >> /etc/cron.daily/ecs.sh
sudo chmod 755 /etc/cron.daily/ecs.sh
cd ~
(crontab -u root -l; echo '0 0 * * * /etc/cron.daily/ecs.sh') | crontab -u root -
sudo yum update -y
The part that does not work is: chmod 755 /etc/cron.daily/ecs.sh
I am not sure, what am I missing.
If you can (have sufficient rights to) create a file, you do not need to sudo to change its permissions to 0755. Which would also likely prompt you to input your password and run non-interactively could be the reason why the action did not take place.
On the other hand, if the user running this did not have the necessary (write) permission, preceding lines creating the file would not happen either.
You also do not need to touch a file, because that > redirection will create it (always a new one).
You also should not cd somewhere and and continue performing actions without checking directory was changed as expected. But since on all action but the unnecessary touch you use absolute path names, you can just as well leave out both cd lines.
If you clean-up the script and it still does not perform expected action, it might be useful (assuming non-interactive execution) to save its output (redirect both standard > (or 1>) and error (2>) output to a file) and examine it for errors.

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

How to properly access network location while executing bash script in cygwin's cron

I've created a bash script to take a backup of a folder to a remote location via cygwin cron however I'm experiencing an issue. The script at the end will execute a command like this one
/usr/bin/tar -zcvf //192.168.1.108/Backup/Folder/Folder.Backup.2015-12-03.1219.tar.gz /cygdrive/d/Folder
Although when I use the command it produces and then executes in the context of a cygwin bash shell it works correctly, when I run it via a cron job it fails because it doesn't recognize the remote location path correctly. If I change the path to a local /cygdrive location or to ~/ it works correctly even via cron so somehow I'm thinking that the network shares are not being correctly viewed by cygwin in it's cron environment.
Any ideas how I could solve this issue?
Here's my bash script
#!/usr/bin/bash
#the path needs to be set to execute gzip command or tar command breaks
export PATH=$PATH:/usr/bin:/bin:/usr/local/bin:/usr/local/sbin:/sbin
if [ $# -ne 3 ]
then
echo "USAGE: backup-clients <path> <name_prefix> <source>";
exit 1;
fi
DATE=`date "+%Y-%m-%d.%H%M"`;
FILEPATH="$1/$2.Backup.$DATE.tar.gz";
COMMAND="/usr/bin/tar -zcvf $FILEPATH $3";
echo "COMMAND="$COMMAND;
eval $COMMAND;
Which I run with the command
/usr/bin/bash /cygdrive/d/mybackupscript.bash "//192.168.1.108/Backup/Folder" "Folder" "/cygdrive/d/Folder"
I really appreciate any help you can provide.

Bash-running a script in background

I have a script(func_test) that works well when i invoke it from my terminal. I need to run the script automatically on boot-up and so i have copied it in /etc/init.d and changed its execution mode and linked it to S99func_test under /etc/rc2.d. But upon reboot I'm getting syntax error in that script. Any idea why I'm getting the error although it works fine with my terminal?
Here is the code used to invoke the script for the 1st time:
#!/bin/bash
cd /opt/bin/
cp func_test /etc/init.d/
cp test_file /etc/init.d
chmod 755 /etc/init.d/func_test
chown root:sys /etc/init.d/func_test
ln /etc/init.d/func_test /etc/rc2.d/S99func_test
ln /etc/init.d/test_file /etc/rc2.d/S99test_file
(the script is dependent on another file(test_file) and i have copied the same to init.d)
Probably you forgot slash at the end of line
cp test_file /etc/init.d/
and I recommend to use ./ to copy files from current directory:
cp ./func_test /etc/init.d/

Tomcat startup script permission on Mac OS X

I'm struggling with a Mac OS X 10.5.8 that I've started using recently for development. I successfully installed tomcat and create launchd.conf for my environment variables.
I believe it works fine. Coz I can build a project with Netbeans using maven and cargo plugins successfully so i found online a script for start and stop the tomcat
#!/bin/bash
case $1 in
start)
sh /Library/apache-tomcat-6.0.20/bin/startup.sh
;;
stop)
sh /Library/apache-tomcat-6.0.20/bin/shutdown.sh
;;
restart)
sh /Library/apache-tomcat-6.0.20/bin/shutdown.sh
sh /Library/apache-tomcat-6.0.20/bin/startup.sh
;;
*)
echo "Usage :start|stop|restart"
;;
esac
exit 0
That script was created in nano in sudo sh
but when i want to run it. is spit out this
sh: /usr/bin/tomcat: Permission denied
I've added chmod 755 *.sh and *.bat inside /Library/apache-tomcat-6.0.20/bin
Still access denied so what do I go around that? I have the admin privileges on the machine.
Thanks for reading
Go to Tomcat bin directory and run the below command:
chmod +x *.sh
This worked for me.
Where did you install the tomcat script to? I'd recommend you install it to /usr/bin. Once installed, make sure the permissions are correct (i.e. chmod 755 /usr/bin/tomcat). You can then confirm with ls -l /usr/bin/tomcat.
If you still get errors once the permissions on /usr/bin/tomcat are correct, then you can add the following two lines following the #!/bin/bash line.
set -x
set -v
With the above lines, bash will output additional information that will allow you to tell what's being executed and where the error is happening.
1) Go to the tomcat directory, which preferably should be "/usr/local/folder-name"
2) Check for the permissions for the folder: ls -l
3) Change the permissions using: sudo chmod -R 755 folder-name
4) Change the owner to the current owner: sudo chown -R owner-name:group-name folder-name
e.g sudo chown -R userName:admin folder-name
Try executing the script again and it should work.

Resources