JAVA_HOME is not set, cannot proceed. (Installing Apache Spark on Linux) - linux

How I am here.
Running Command:
opt/develop/spark-3.3.1$ sudo ./dev/make-distribution.sh -pHADOOP-3 Dhadoop.version=3.2.4 -Pyarn --name custom-spark --pip --r --tgz -Psparkr -Phive -Phive-thriftserver -Pmesos -Pyarn -Pkubernetes
The script (make-distribution.sh), apparently, is not reading the value of the variable i.e., $JAVA_HOME.
Running Command:
/opt/develop/spark-3.3.1$ echo $JAVA_HOME
Shows expected result: /usr/lib/jvm/jdk-19.0.1
Attempted:
Well, added to the file /opt/develop/spark-3.3.1/sbin/spark-config.sh
export JAVA_HOME="/usr/lib/jvm/jdk-19.0.1" export PATH=$PATH:$JAVA_HOME/bin
did not seem to change the result of Running command.
Logging-out and logging back in is also of no avail.
Note-1:
My JAVA_HOME is also set in /etc/profile.d/my-envvars file as:
export JAVA_HOME="/usr/lib/jvm/jdk-19.0.1" export PATH=$PATH:$JAVA_HOME/bin
Note-2: commands java --version and javac --version appears to be working okay.
The file: /opt/develop/dev/make-distribution.sh
Reads:
`
set -o pipefail
set -e
set -x
# Figure out where the Spark framework is installed
SPARK_HOME="$(cd "`dirname "$0"`/.."; pwd)"
DISTDIR="$SPARK_HOME/dist"
MAKE_TGZ=false
MAKE_PIP=false
MAKE_R=false
NAME=none
MVN="$SPARK_HOME/build/mvn"
function exit_with_usage {
set +x
echo "make-distribution.sh - tool for making binary distributions of Spark"
echo ""
echo "usage:"
cl_options="[--name] [--tgz] [--pip] [--r] [--mvn <mvn-command>]"
echo "make-distribution.sh $cl_options <maven build options>"
echo "See Spark's \"Building Spark\" doc for correct Maven options."
echo ""
exit 1
}
# Parse arguments
while (( "$#" )); do
case $1 in
--tgz)
MAKE_TGZ=true
;;
--pip)
MAKE_PIP=true
;;
--r)
MAKE_R=true
;;
--mvn)
MVN="$2"
shift
;;
--name)
NAME="$2"
shift
;;
--help)
exit_with_usage
;;
--*)
echo "Error: $1 is not supported"
exit_with_usage
;;
-*)
break
;;
*)
echo "Error: $1 is not supported"
exit_with_usage
;;
esac
shift
done
if [ -z "$JAVA_HOME" ]; then # THIS SHOULD EVALUATE FALSE.
# Fall back on JAVA_HOME from rpm, if found
if [ $(command -v rpm) ]; then
RPM_JAVA_HOME="$(rpm -E %java_home 2>/dev/null)"
if [ "$RPM_JAVA_HOME" != "%java_home" ]; then
JAVA_HOME="$RPM_JAVA_HOME"
echo "No JAVA_HOME set, proceeding with '$JAVA_HOME' learned from rpm"
fi
fi
if [ -z "$JAVA_HOME" ]; then
if [ `command -v java` ]; then
# If java is in /usr/bin/java, we want /usr
JAVA_HOME="$(dirname $(dirname $(which java)))"
fi
fi
fi
if [ -z "$JAVA_HOME" ]; then # This SHOULD EVALUATE FALSE. APPARENTLY, IT DOES NOT
echo "Error: JAVA_HOME is not set, cannot proceed."
# I Should not be here, but I am!
exit -1
fi
`

Your problem lies within the fact that you're executing your script with sudo, which does not preserve environment variables by default.
As a small demonstration, consider the following:
someone#somewhere:~/my-path$ export SOMEVAR="testmyvar"
someone#somewhere:~/my-path$ echo $SOMEVAR
testmyvar
someone#somewhere:~/my-path$ sudo bash -c 'echo $SOMEVAR'
As you can see, when executing a command with sudo, you don't preserve all of your environment variables.
There is luckily quite an easy fix for that: use sudo -E (that does preserve environment variables, you can find that option in the man page of sudo)
So in our little example a bit higher:
someone#somewhere:~/my-path$ sudo -E bash -c 'echo $SOMEVAR'
testmyvar
You see that the -E option does what you want it to do!
Hope this fixes your problem :)

Related

Bash script to create virtual clusters isn't working

I am trying to create a script to create virtual clusters on my virtual machine which is a CentOS 7 minimal.
I got a script named cluster
#!/bin/bash
function vc
{
echo
echo -n "Enter project name: "
read platform_name
echo
echo -n "web extension: "
read web_extension
echo
echo -e "The following website will be created"
echo -e "\e[32m Platform:\e[0m\t${platform_name}"
echo -e "\e[32m Extension:\e[0m\t${web_extension}"
echo -e "\e[32m Full URL:\e[0m\t http://www.${platform_name}.${web_extension}"
echo
echo -e "Do you wish to proceed? [Y/n]"
read -p "Are you sure? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo
echo -e "\e[32m Creating platform \e[0m\t"
else
echo
echo -e "\e[32m Not creating platform \e[0m\t"
fi
}
if [ -n "$(type -t $FUNCTION_NAME)" ] && [ "$(type -t $FUNCTION_NAME)" =
function ];
then $FUNCTION_NAME $2; else help; fi
Then as far as I understood I just have to make it executable
chmod +x cluster
And after this I should make a syslink for it ln -s cluster /bin/cluster
And now I should normally be able to just typ cluster vc in the terminal and it should execute the script but it keeps giving me "command cluster not found"
Am I doing something obviously wrong? Or do I need to use another chmod on it so I can run this?
Symbolic link targets are resolved relative the the symlink location. In your case that means, if you run /bin/cluster it looks for a file named cluster (the target) in the /bin/directory. Either provide a relative path which points to your file or link to an absolute path: ln -s /path/to/cluster /bin/cluster.
Also make sure that the target location is readable and executable by whomever executes the symlink.

Bash script creating hybrid iso generates “unexpected end of file”

I've created a simple bash script that creates a hybrid iso. But when I try to run it, I get the output:
hybridiso.sh: line 58: syntax error: unexpected end of file.
I've checked the script and tried to makes changes to it but I still get the same output. What's wrong with the script?
#!/bin/bash
##Sanity Cheks##
if [ "$(whoami)" != root ]; then
echo "You must be root to execute this script."
fi
if [ ! -x /usr/bin/xorriso ]; then
echo "xorriso is not installed. Run 'apt-get install xorriso' to install it."
exit 1
fi
if [ ! -x /usr/bin/live-build ]; then
echo "live-build is not installed. Run 'apt-get install live-build' to install it."
exit 1
fi
if [ ! -x /usr/bin/syslinux ]; then
echo "syslinux is not insatlled. Run 'apt-get install syslinux' to install it."
exit 1
fi
if [ ! -x /usr/bin/mksquashfs ]; then
echo "squashfs-tools is not installed. Run 'apt-get install squashfs-tools' to install it."
exit 1
fi
###############
mkdir $PWD/hybridiso
cd hybridiso
mkdir -p binary/live && mkdir -p binary/isolinux
read -e -p "Enter local file path for linux kernel " kernel
read -e -p "Enter local file path for initrd " initrd
cp $kernel binary/live/ && cp $initrd binary/live/
#mksquashfs chroot binary/live/filesystem.squashfs -comp xz -e boot
cp /usr/lib/syslinux/isolinux.bin binary/isolinux/
cp /usr/lib/syslinux/menu.c32 binary/isolinux/
while true; do
read -p "Do you have an isolinux.cfg? " resp
if [ $resp -eq no ]; then
echo "You need to create a valid isolinux.cfg file!"
echo "Creating example file $PWD/isolinux.cfg.example"
echo -e "ui menu.c32\nprompt 0\nmenu title Boot Menu\ntimeout 300\n\n\nlabel live-amd64\n menu label ^Live (amd64)\n menu default\n linux /live/linux\n append initrd=/live/initrd.gz boot=live persistence quiet\n\n\nlabel live-amd64-failsafe\n menu label ^Live (amd64 failsafe)\nlinux /live/linux\nappend initrd=/live/initrd.gz boot=live persistence config memtest noapic noapm nodma nomce nolapic nomodest nosmp nosplash vga=normal\n\n\nendtext" >> isolinux.cfg.example
exit 1
elif [ $resp -eq yes ]; then
break
else
"Put only in yes or no"
fi
read -e -p "Enter local file path for isolinux.cfg" isolinux
cp $isolinux binary/isolinux/
xorriso -as mkisofs -r -J -joliet-long -l -cache-inodes -isohybrid-mbr /usr/lib/syslinux/isohdpfx.bin -partition_offset 16 -A "Debian Live" -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o kiosk.iso binary
echo "Script is succesfull!"
exit 0
You haven't terminated the while loop with done anywhere; Add done at the appropriate location.

Bash script output not going to stdout

I have a build process, kicked off by Make, that executes a lot of child scripts.
A couple of these child scripts require root privileges, so instead of running everything as root, or everything as sudo, I'm trying to only execute the scripts that need to be as root, as root.
I'm accomplishing this like so:
execute_as_user() {
su "$1" -s /bin/bash -c "$2;exit \$?"
}
Arg $1 is the user to run the script as, arg $2 is the script.
Arg $1 is either root (gotten with: $(whoami) since everything is under sudo), or the current user's account (gotten with: $(logname))
The entire build is kicked off as:
sudo make all
Sample from the Makefile:
LOG="runtime.log"
ROTATE_LOG:=$(shell bash ./scripts/utils/rotate_log.sh)
system:
/bin/bash -c "time ./scripts/system.sh 2>&1 | tee ${LOG}"
My problem is... none of the child scripts are printing output to stdout. I believe it to be some sort of issue with an almost recursive call of su root... but I'm unsure. From my understanding, these scripts should already be outputting to stdout, so perhaps I'm mistaken where the output is going?
To be clear, I'm seeing no output in either the logfile nor displaying to the terminal (stdout).
Updating for clarity:
Previously, I just ran all the scripts either with sudo or just as the logged in user... which with my makefile above, would print to the terminal (stdout) and logfile. Adding the execute_as_user() function is where the issue cropped up. The scripts execute and build the project... just no display "that it's working" and no logs.
UPDATE
Here is some snippets:
system.sh snippet:
execute_script() {
echo "Executing as user $3: $2"
RETURN=$(execute_as_user $3 ${SYSTEM_SCRIPTS}/$2)
if [ ${RETURN} -ne ${OK} ]
then
error $1 $2 ${RETURN}
fi
}
build_package() {
local RETURN=0
case "$1" in
system)
declare -a scripts=(\
"rootfs.sh" \
"base_files.sh" \
"busybox.sh" \
"iana-etc.sh" \
"kernel.sh" \
"firmware.sh" \
"bootscripts.sh" \
"network.sh" \
"dropbear.sh" \
"wireless_tools.sh" \
"e2fsprogs.sh" \
"shared_libs.sh"
)
for SCRIPT_NAME in "${scripts[#]}"; do
execute_script $1 ${SCRIPT_NAME} $(logname)
echo ""
echo -n "${SCRIPT_NAME}"
show_status ${OK}
echo ""
done
# finalize base system
echo ""
echo "Finalizing base system"
execute_script $1 "finalize.sh" $(whoami)
echo ""
echo -n "finalize.sh"
show_status ${OK}
echo ""
# package into tarball
echo ""
echo "Packing base system"
execute_script $1 "archive.sh" $(whoami)
echo ""
echo -n "archive.sh"
show_status ${OK}
echo ""
echo ""
echo -n "Build System: "
show_status ${OK}
;;
*)
echo "$1 is not supported!"
exit 1
esac
}
sample child script executed by system.sh
cd ${CLFS_SOURCES}/
tar -xvjf ${PKG_NAME}-${PKG_VERSION}.tar.bz2
cd ${CLFS_SOURCES}/${PKG_NAME}-${PKG_VERSION}/
make distclean
RESPONSE=$?
if [ ${RESPONSE} -ne 0 ]
then
pkg_error ${RESPONSE}
exit ${RESPONSE}
fi
ARCH="${CLFS_ARCH}" make defconfig
RESPONSE=$?
if [ ${RESPONSE} -ne 0 ]
then
pkg_error ${RESPONSE}
exit ${RESPONSE}
fi
# fixup some bugs with musl-libc
sed -i 's/\(CONFIG_\)\(.*\)\(INETD\)\(.*\)=y/# \1\2\3\4 is not set/g' .config
sed -i 's/\(CONFIG_IFPLUGD\)=y/# \1 is not set/' .config
etc...
Here's the entire system.sh script:
https://github.com/SnakeDoc/LiLi/blob/master/scripts/system.sh
(i know the project is messy... it's a learn-as-you-go style project)
Previously, I just ran all the scripts either with sudo or just as the
logged in user... which with my makefile above, would print to the
terminal (stdout) and logfile. Adding the execute_as_user() function
is where the issue cropped up. The scripts execute and build the
project... just no display "that it's working" and no logs.
Just a guess, but you're probably not calling your function or not calling it properly:
execute_as_user() {
su "$1" -s /bin/bash -c "$2;exit \$?"
}
execute_as_user "$#"
I also noticed that you're not passing any argument to the script at all. Is this meant?
./scripts/system.sh ???

Bash script: Syntax error in conditional expression

I'm new around the neighborhood and stuck with a syntax error. Please take a look and maybe someone can assist. I'm trying to run the following script:
#!/bin/bash
main () {
dpkg -query -s $1 &> /tmp/pkg_verify
if grep -q 'not installed' /tmp/verify
then
echo -e "\e[31m$1 is not installed. installing..\e[0m"
apt-get install $1
echo -e "\e[31m$1 is not installed and ready to use\e[0m"
else
echo -e "\e[31m$1 is already installed\e[0m"
fi
rm -f /tmp/pkg_verify
for test in $#; do main $test; shift; done
echo -e "\e[31mDone\e[0m"
}
for test in $#; do main $test; shift; done
echo -e "\e[31mDone\e[0m"
But when I try to execute it I'm facing with endless loop:
grep: /tmp/verify: No such file or directory
16 is already installed
I truly tried to find the answer, tried to change the if to couple of different forms but with out any success. Does any one have an idea why that is? What should I change so that the script can run?
Thanks in advance to all the helpers.
You have two else following each other. That can't work. It's either elif condition or just a single else.
The infinite loop is caused by main calling itself recursively.
And third, it's probably a bug to shift when iterating with for i in "$#".
To debug a script (free of syntax errors) use set -x near the beginning.
Replace this line:
if [[ -z `grep 'not installed' /tmp/pkg_verify` ]]
with this if condition:
if grep -q 'not installed' /tmp/pkg_verify
Full Script:
main () {
dpkg-query -s "$1" > /tmp/pkg_verify
if grep -q 'not installed' /tmp/pkg_verify
then
echo -e "\e[31m$1 is not installed. installing..\e[0m"
apt-get install "$1"
echo -e "\e[31m$1 is not installed and ready to use\e[0m"
else
echo -e "\e[31m$1 is already installed\e[0m"
fi
}
rm -f /tmp/pkg_verify
for test in $#; do main "$test"; done
echo -e "\e[31mDone\e[0m"

SSH through Putty comes up with a "no such file or directory"

I'm trying to access out Linux server through Putty but for some reason after a somewhat successful login it just throws a "/bin/bash no such file or directory" then Putty closes.
Contents of bashrc:
# /etc/bashrc
# System wide functions and aliases
# Environment stuff goes in /etc/profile
# By default, we want this to get set.
# Even for non-interactive, non-login shells.
if [ $UID -gt 99 ] && [ "`id -gn`" = "`id -un`" ]; then
umask 002
else
umask 022
fi
# are we an interactive shell?
if [ "$PS1" ]; then
case $TERM in
xterm*)
if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
else
PROMPT_COMMAND='echo -ne "\033]0;${USER}#${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
fi
;;
screen)
if [ -e /etc/sysconfig/bash-prompt-screen ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
else
PROMPT_COMMAND='echo -ne "\033_${USER}#${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\033\\"'
fi
;;
*)
[ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
;;
esac
# Turn on checkwinsize
shopt -s checkwinsize
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u#\h \W]\\$ "
fi
if ! shopt -q login_shell ; then # We're not a login shell
# Need to redefine pathmunge, it get's undefined at the end of /etc/profile
pathmunge () {
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}
# Only display echos from profile.d scripts if we are no login shell
# and interactive - otherwise just process them to set envvars
for i in /etc/profile.d/*.sh; do
if [ -r "$i" ]; then
if [ "$PS1" ]; then
. $i
else
. $i >/dev/null 2>&1
fi
fi
done
unset i
unset pathmunge
fi
# vim:ts=4:sw=4
What could be causing this?
Thanks!
Well bash is more than likely located at a different location on this linux box to /bin/bash
I've found this to be the case on different boxes and I've changed my .profile script in my home directory to not directly execute bash, could be a solution in your case. So when you log in you stay in the bourne shell, and then go into the bash shell only if you explicitly type bash.
Check your /etc/ssh/sshd_config to make sure that you don't have a chroot directory set. If you do, you will need to either create a bin directory in the chroot directory and either copy or link the necessary binaries into that directory.
Or you could always comment that line out in the config.
Either way, restart sshd and test.

Resources