Unable to execute the comand on the remote Linux hosts

I have multiple hosts ( More than 20) and need to execute the command but it is not working as expected.

  1. This snippet of the code runs on multiple hosts and gives me the process id on the host where i am running .
      for hsts in `cat /a/b/c//hsts.txt`  # hsts.txt file contains all the hosts
         do
         ssh ${id}@${hsts} "cat /z/y/x|tail -1" | awk '{print $30}'
        done
  1. I need to modify the above command where i need to see the process which is running based on the process id which i am getting from above snippet of the code.

    The below snippet runs on individual hosts and it is displaying me the process but if i incorporate the above in the for loop it is not working . Appreciate your help

    on this.

   ps -ef |grep `cat /z/y/x|tail -1 |awk '{print $30}'`

Hi Jack and welcome to the forums.
Please use code or icode tags.

Hope this helps

EDIT2:
Why would you need the PID of a task that probably doesnt even take 1s to execute?

You can try to embed your remote script in a here document, then for faster processing store it in a variable.

scriptcode=$(
cat << "ENDSCRIPT"
echo "running on `uname -n`"
ps -fp `grep \`tail -1 /z/y/x | awk '{print $30}'\``
ENDSCRIPT
)

echo "scriptcode is
$scriptcode"

for hsts in `cat /a/b/c//hsts.txt`  # hsts.txt file contains all the hosts
do
   ssh ${id}@${hsts} "$scriptcode"
done