Running pwdx $(pidof java) command on remote nodes

Nov 27, 2018 12:25 · 168 words · 1 minute read

Script to get output of pwdx $(pidof java) command

#!/bin/bash

if [ "$1" ]; then
echo "you are good to go"
else
echo "Usage: $(basename "$0") takes an ip file as input"
exit
fi

if [ -s "$1" ]; then
echo "Good!Got something inside the file"

while IFS= read -r line # IFS= before read to avoid removing leading and trailing spaces

do {
remote_output="$(ssh -qni <path-to-your-private-ssh-key> <username>@"$line" "pgrep java | xargs pwdx")"
echo -e "$line""\n""$remote_output""\n" | tee -a pwdx_output.txt
} done < "$1"

else
echo "$1 is empty."
fi

Useful Code Snippets

# '/sbin/pidof java | xargs pwdx' works however 'pidof java | xargs pwdx' doesn't. why?
# Because bash is not able to find pidof command
# Also 'pwdx $(pidof java)' doesn't work due to creation of new subshell. HW: do the RCA.

## Another Way
#cat "$1" | while IFS= read -r line
#do {
#remote_output="$(ssh -qi <path-to-your-private-ssh-key> <username>@"$line" "pgrep java | xargs pwdx")"
#echo -e "$line""\n""$remote_output""\n"
#} < /dev/null; done

Suggested readings

tweet Share