Skip to content
Advertisement

top and grep output nothing when run from shell script

I am trying to create shell script that ssh into a remote server and run a script there and print the output in the local server but when I run the script in the local server it most of the time outputs nothing and rarely outputs data :

Mule: CPU > % RAM > %

and when I ssh in the local server to the remote server in the command line and run the script it outputs normally in the command line :

Mule: CPU > 39.0% RAM > 8.1%

the script in local server

#!/bin/bash
echo -e 'r' 
echo 'leg3'
echo -e 'r'
ssh  -qT appread@${remote} << EOF
source /home/appread/Process_mon.sh 
exit
EOF

script in remote server :

#!/bin/bash
mulecpu=$(top -b -n 1 -c  | grep  -P '.*[j]ava.*mule.*'|  awk '{print $9}')
muleram=$(top -b -n 1 -c  | grep -P '.*[j]ava.*mule.*'|  awk '{print $10}')
m=$(echo 'Mule: CPU > '$mulecpu'% RAM > '$muleram'% ')
echo $m

Advertisement

Answer

If you run top without -w, its output may be truncated and so your grep may fail.

Add -w 512 or similar to maximise the width of the output:

#!/bin/bash

top -b -n 1 -c -w 512 |
awk '/[j]ava.*mule/ { printf "Mule: CPU > %s%% RAM > %s%%n",$9,$10 }'


User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement