Linux shell script "unable to access jarfile" - linux

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.

Related

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

How to run WP-CLI using bash script in linux

I'm having issue to run bash script setting up WP-CLI by it's own. Keep on getting wp no command found error. Please help.
#!/bin/bash
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
exec bash
wp --info
wp plugin install taxonomy-terms-order --path=/var/www
wp plugin activate taxonomy-terms-order --path=/var/www
It's only running till exec bash line. after that its not installing any plugin. Please help.
Do not hesitate to make small experiments to understand the problem:
$ cat test.sh
#!/bin/bash
echo "Test 1"
exec bash
echo "Test 2"
$ echo $$
6506
$ ./test.sh
Test 1
$ echo $$
6548
exec bash is opening a new blocking process.
So, I think you can remove this line from your script.
If /usr/local/bin is not in your PATH, you can use the complete path of /usr/local/bin/wp instead of wp :
/usr/local/bin/wp --info
/usr/local/bin/wp plugin install taxonomy-terms-order --path=/var/www
/usr/local/bin/wp plugin activate taxonomy-terms-order --path=/var/www
Or you can add this path to the PATH:
export PATH="${PATH}:/usr/local/bin/wp"

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

Can't run shell script from other shell script

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

Resources