Background SSH using here document

I'm trying to use Here documents to pass several commands to a remote server over SSH. However I'm having problems. The code is as follows:

nohup ssh $i_hostname exec /usr/bin/ksh -s << EOF >> $AS_STOPSAP_LOG &
echo $i_instname;
ps -ef | grep name | grep ${i_instname} |grep -v grep
 
proc=$(ps -ef | grep name | grep ${i_instname} |grep -v grep
| awk '{print $2}')
 
$(
for j in $STARTSRV
do
echo loop checking item $j
kill -9 $j;
echo "Killed $j"
done
)
EOF

The problem seems to start with the fact that the "proc" variable does not seem to set correctly. I thought you could include $() for sub-shells via EOF docs? Any ideas anyone?

where is the i_instname variable set?

it's set in the greater programme so it's OK. The sub-script within $() is the problem I believe. It seems to me that it's evaluated before the here doc is passed to the SSH --- I want it to be run on the target server ...

Either scp and script and at or batch it using ssh on the remote machine, or put the entire command inside single tics (red):

nohup ssh $i_hostname ' exec /usr/bin/ksh -s << EOF >> $AS_STOPSAP_LOG &
echo $i_instname;
ps -ef | grep name | grep ${i_instname} |grep -v grep
 
proc=$(ps -ef | grep name | grep ${i_instname} |grep -v grep
| awk '{print $2}')
 
$(
for j in $STARTSRV
do
echo loop checking item $j
kill -9 $j;
echo "Killed $j"
done
)
EOF'
1 Like

I found a way to alter it - have to have here doc (EOF) in speech marks ; "EOF"
so that it's all sent as plain text (nothing is exapanded etc.) so that any commands run on the remote host - not the local one.
then pass parameters to it for anything that has to be dynamically set locally and passed in.

See this:

nohup ssh $i_hostname exec /usr/bin/ksh -s ${i_instname} << "EOF" >> $AS_STOPSAP_LOG &
i_instname=${1}
ps -ef | grep name | grep ${i_instname} |grep -v grep
 
procs=$(ps -ef | grep name | grep ${i_instname} |grep -v grep | awk '{print $2}')
 
for j in $procs
 do
 kill -9 $j;
 echo "Killed $j"
done
EOF