Can't run shell script from other shell script - linux

I'm trying to run a shell script that calls another shell script:
#!/bin/sh
for k in `cat ../config/file1.keywords`
do
echo "*** keyword: $k ***"
./file2.sh $k
done
I got the following error:
./file2.sh: Permission denied
I tried using: chmod +x file2.sh
When I ran my script again, I got a different error:
./file2.sh: not found
What am I missing here...? Thanks.
(I'm running on a ubuntu 14.04 LTS on an Oracle VM VirtualBox)

have you something like #!/bin/sh in file2.sh? If not, use sh file2.sh $kinstead of ./file2.sh $k

Related

Linux shell script "unable to access jarfile"

I have a jar file in the /root directory of a debian 11 VPS. I am having trouble creating a startup shell script.
The contents of the script (/etc/init.d/runjar.sh) are as follows:
#!/bin/sh
echo "Running Jar"
java -jar /root/bot.jar
exit 0
I had ran both "chmod +x /etc/init.d/runjar.sh" and "update-rc.d runjar.sh defaults". When I restarted the VPS, the jar did not run.
I tried running the script through the terminal "sh /etc/init.d/runjar.sh" and was met with the response:
root#api:~# sh /etc/init.d/runjar.sh
: not found/runjar.sh: 2:
Running Jar
Error: Unable to access jarfile /root/bot.jar
: not found/runjar.sh: 5:
I have made sure the permissions were set using "chmod +x /root/bot.jar" and "chmod 777 /root" to no avail.
Any help would be appreciated.
Instead of using sh you can use the service command because the runjar.sh is palced in the init.d folder.
1. run a new script for a test
echo -e '#!/bin/sh\necho "Running Jar"\ndate && echo "successful"' > /etc/init.d/runjar.sh
sh /etc/init.d/runjar.sh
if output info is successful,then it proved the env of shell is fine
2. rewrite your script
echo -e '#!/bin/sh\necho "Running Jar"\njava -jar /root/bot.jar\nexit 0' > /etc/init.d/runjar.sh
chmod +x /etc/init.d/runjar.sh
sh /etc/init.d/runjar.sh
and check the output info.

Shell Script won't run from NodeJS because it has "set -o pipefail" [duplicate]

The below mentioned line of code used to work for me all the time on a Ubuntu 16.04 distribution, but suddenly option-name pipefail is an illegal option:
set -eu -o pipefail
returns:
set: Illegal option -o pipefail
Why does this happen? I run the command on a completely new installed system and as part of a shell script. The code is placed right at the beginning:
myscript.sh:
1 #!/bin/bash
2 set -eu -o pipefail
3 ...
The script is run as sudo:
sudo sh ./myscript.sh
You are running bin/sh, on Ubuntu it is a symbolic link pointing to /bin/dash, but pipefail is a bashism.
Make the script executable:
chmod +x myscript.sh
and then run the script as follows:
sudo ./myscript.sh
I had the same error when running script from zsh and the script began with incorrect shebang.
WRONG, missing ! after #:
#/bin/bash
rest-of-the-script
Correct:
#!/bin/bash
rest-of-the-script

Running linux commands inside bash script throws permission denied error

We have linux script in our environment which does ssh to remote machine with a common user and copies a script from base machine to remote machine through scp.
Script Test_RunFromBaseVM.sh
#!/bin/bash
machines = $1
for machine in $machines
do
ssh -tt -o StrictHostKeyChecking=no ${machine} "mkdir -p -m 700 ~/test"
scp -r bin conf.d ${machine}:~/test
ssh -tt ${machine} "cd ~/test; sudo bash bin/RunFromRemotevm.sh"
done
Script RunFromRemotevm.sh
#!/bin/bash
echo "$(date +"%Y/%m/%d %H:%M:%S")"
Before running Test_RunFromBaseVM.sh script base vm we run below two commands.
eval $(ssh-agent)
ssh-add
Executing ./Test_RunFromBaseVM.sh "<list_of_machine_hosts>" getting permission denied error.
[remote-vm-1] bin/RunFromRemotevm.sh:line 2: /bin/date: Permission denied
any clue or insights on this error will be of great help.
Thanks.
I believe the problem is the presence of the NOEXEC: tag in the sudoers file, corresponding to the user (or group) that's executing the "cd ~/test; sudo bash bin/RunFromRemotevm.sh" command. This causes any further execv(), execve() and fexecve() calls to be refused, in this case it's /bin/date.
The solution is obviously remove the NOEXEC: from the main /etc/sudoers file or some file under /etc/sudoers.d, whereever is this defined.

mvn command not found when ran in init.d service

I'm facing an issue with creating init.d service. Following is my run.sh file which executes completely fine (As root user)
mvn install -DskipTests
mvn exec:java
But when I execute same file as service in init.d (service run start). I get
mvn command not found
Following is my start method
start() {
if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service…' >&2
CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
su -c "$CMD" $RUNAS > "$PIDFILE"
echo 'Service started' >&2
}
Link to complete script which i'm using
https://gist.githubusercontent.com/naholyr/4275302/raw/9df4ef3f4f2c294c9585f11d1c8593b62bdd52d3/service.sh
RUN AS value is set as root
When you run a command using sudo you are effectively running it as the superuser or root.
The reason that the root user is not finding your command is likely that the PATH environment variable for root does not include the directory where maven is located (quite evident as in the comments). Hence the reason for command not found error.
Add PATH to your script and that it includes /opt/inte‌​gration/maven/apache‌​-maven-3.3.9/bin. Since the init script won't share the PATH environment variable with the rest of the system (since it being run much ahead of the actual updates of $PATH in the .bash_profile) you need to set it directly on your script and make sure maven is in there, for example, add the below line to the init script in the beginning.
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin‌​:/root/bin:/opt/inte‌​gration/maven/apache‌​-maven-3.3.9/bin

bash & s3cmd not working properly

Hi I have a shell script which contains s3cmd command on ubuntu 12.04 LTS.
I configured cron for this shell script which works fine for local environment but don't push the file to s3. But when i run shell script manually, It pushes the file to s3 without any error. I checked log and found nothing for this. Here is my shell script.
#!/bin/bash
User="abc"
datab="abc_xyz"
pass="abc#123"
Host="abc1db.instance.com"
FILE="abc_rds`date +%d_%b_%Y`.tar.gz"
S3_BKP_PATH="s3://abc/db/"
cd /abc/xyz/scripts/
mysqldump -u $User $datab -h $Host -p$pass | gzip -c > $FILE | tee -a /abc/xyz/logs/app-bkp.log
s3cmd --recursive put /abc/xyz/scripts/$FILE $S3_BKP_PATH | tee -a /abc/xyz/logs/app-bkp.log
mv /abc/xyz/scripts/$FILE /abc/xyz/backup2015/Database/
#END
This is really weird. Any suggestion would be a great help.
Check if the user running configured in crontab has correct permissions and keys in the environment.
I am guessing the keys are configured in env file as they are not here in the script.

Resources