mvn command not found when ran in init.d service - linux

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

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.

Finding an application within the system files

I want the script to be able to find an application within the whole file system as the application is not always installed in the /Applications folder. I want to find it to remove it. Currently I have a script that looks in the users application folder and the general applications folder which is shared by all accounts. However, the application is sometimes stored in a different place.
currently the script checks if the process (the application) is running, if not then delete the application from the specified folder.
Is there any way to do a general sweep ?
#!/bin/bash
process="$4"
#check if abc is running
if pgrep $process >/dev/null 2>&1
then
echo $process is running
exit 1
else
echo $process is not running - remove all versions
#Remove ALL versions of Skype for Business.app
rm -rf $HOME/Applications/"Skype for Business.app"
rm -rf /Applications/"Skype for Business.app"
#Trigger the skype install policy
sudo jamf policy -event installSkype
echo "triggering install policy for Skype"
fi

Ask user to run makefile from root mode

I have a makefile and configure shell in my project. I wrote code to ask user to run configure shell in root mode using the following code.
[ "$(whoami)" != "root" ] && exec sudo -- "$0" "$#"
But when I run 'make install'
I need to ask user to run from root mode.
So I just copied the code from the configure shell and copied it in another shell script file called 'runasroot.sh'. Then I run this shell script from the make install.
install:
#echo About to install XXX Project
./runasroot.sh
find . -name "*.cgi" -exec cp {} $(SCRIPTDEST)/ \;
When I run the above code, I got the below error.
About to install XXX Project
./runasroot.sh \;
make: *** [install] Error 1
runasroot.sh
#!/bin/bash
[ "$(whoami)" != "root" ] && exec sudo -- "$0" "$#"
target:
#if ! [ "$(shell id -u)" = 0 ];then
#echo "You are not root, run this target as root please"
exit 1
fi
Explanations
You can use ifneq to check that the user is not root and echo a message for instance, not performing the actual action is the user was indeed a root user. Since the id of the root user is usually 0 on a UNIX-like operating system, we can check that the user ID is 0 (or not) in a conditional.
Proposal solution
install:
ifneq ($(shell id -u), 0)
#echo "You must be root to perform this action."
else
#echo "TODO: The action when the user is root here..."
endif
Output
$ make install
You must be root to perform this action.
$ sudo make install
TODO: The action when the user is root here...
External resources
Conditional Parts of Makefiles
The shell Function
Recipe Echoing
Man for the id function
What is a root user
There are several mistakes done here.
The first problem is what you're trying to do. It is very bad form to require root as part of the build process. For the build part, try to not require root for compiling anything. If you need to create special files as part of the packaging, use fakeroot or fakeroot-ng to get the same effect without any actual privilege escalation.
For install, just let the user run your entire make file as root, if she so chooses. Many operations that usually require root sometimes don't. For example, make install does not require root if the install is to a DESTDIR where the user has privileges.
If you are dead set on doing this anyway, however, your flow is completely wrong. While runasroot.sh does exactly what you want it to do, it only does so for itself. When you look at your make receipt:
install:
#echo About to install XXX Project
./runasroot.sh # <<-- this line runs as root
find . -name "*.cgi" -exec cp {} $(SCRIPTDEST)/ \;
The runasroot.sh line runs as root, but the find line is a different process, and is completely unaffected.
That would be true for regular shell script, but it's doubly true for a make receipt. In a shell script, each command gets its own process. In a make receipt, each command gets its own shell. Your runasroot.sh does not, and cannot, affect the privileges under which the find runs.
So, what you are trying to do is both impossible and unwanted. Just try to install and fail if you don't have enough permissions.
Automatically call make again with sudo and all parameters preserved if user is not root:
install:
ifneq ($(shell id -u), 0)
sudo make $#
else
echo Your commands here...
endif

Jenkins adding single quotes to bash shell script

My shell script looks like this:
#!/bin/bash
USER=$1
sudo rm -rf /home/$USER/system/logs/*
exit 0
It's checked into cvs in a shell folder, Jenkins is configured to execute it on a Linux machine via a job with 'Execute Shell' build step:
bash -ex shell/clear-logs.sh myuser
But Jenkins is wrapping the whole sudo line in single quotes which results in my log files not been deleted (although the Jenkins job passes successfully):
[workspace] $ /bin/sh -xe /tmp/hudson7785398405733321556.sh
+ bash -ex shell/clear-logs.sh myuser
+ USER=myuser
+ sudo rm -rf '/home/myuser/system/logs/*'
+ exit 0
Any ideas why Jenkins is doing this? If I call the script from the Jenkins workspace location as the root user, then it works fine.
EDIT:
I have the same shell script, in different cvs modules, being executed by Jenkins on the same linux server. Have created a new job, either as freestyle or by copying an existing job where this works, but makes no difference.
Okay, seemed to have resolved this by adding the 'jenkins' user to the 'myuser' group and restarting the jenkins service. If the logs directory is empty, then Jenkins console output does report the path in single quotes, as no files found. But run the job a second time where there are files, and no single quotes, files correctly deleted.
Jenkins is not doing anything with your quotation marks, such as changing double to single - you are seeing the output of set -x. Try this in your shell:
set -x
ls "some string with spaces"
Output will be something like:
+ ls --color=auto 'some string with spaces'
bash is just showing you debug output of its interpretation and tokenization of your command.
Adapt the permissions of /home/$USER/... I got the following in the Console Output at first:
+ USER=geri
+ rm -rf '/home/geri/so-30802898/*'
rm: cannot remove ‘/home/geri/so-30802898/*’: Permission denied
Build step 'Execute shell' marked build as failure
After adapting the permissions the build/deletion succeeded.

ssh command to run remote script exist shell on remote server when switching user

When I run a script such as this:
ssh -t root#10.10.10.10 '/tmp/somescript.sh'
where the script is defined as:
#!/bin/sh
mkdir -p /data/workday/cred
chown -R myuser:myuser /data
su myuser - # <------- NOTICE THIS ! ! ! !
rpm -Uvp --force --nodeps --prefix /data/place /data/RPMs/myrpm.rpm
Notice the above su command.
If I comment-out the su command, the script runs remotely and then my shell prompt returns to where I came from ( same server where I ran the ssh command above )
But leaving the script as listed above, causes the script to complete successfully but the shell prompt stays on the remote server.
How can I prevent that ? Making sure that the issuer of the rpm command is a different user than root just a listed ?
But leaving the script as listed above, causes the script to complete successfully but the shell prompt stays on the remote server.
Not exactly. The script is running up to the su command, which spawns a new subshell, and stopping there until you exit the shell. Until you exit that shell, the rpm command never runs, and when it does, it runs as root.
If you want to run the rpm command as a non-root user, you'd need to do something a little different, like:
sudo -u myuser rpm -Uvp ...
add 'exit' at the end of your script

Resources