This question already has an answer here:
Executing ssh command in a bash shell script within a loop [duplicate]
(1 answer)
Closed 9 years ago.
I am Oracle DBA and i am doing some scripting to check if database is live or not on our large number of servers.
I am trying to set value to variable in ssh -X e.g
[oracle#proddb02]$ DB_STAT=`ssh -X proaddb01 'ps -ef | grep pmon | grep -v grep' | awk '{ print $8 }'`
[oracle#proddb02]$ echo $DB_STAT
ora_pmon_pconn01
Above example works perfectly but as i am using ssh command i cannot loop using while as it exit at the first line file which include list of the servers.
so i have to add /dev/null to the command so that it should not exit the loop. but this does not set the variable value.
When i echo the variable it gives nothing.
[oracle#proddb02]$ DB_STAT=`ssh -X proddb01 'ps -ef | grep pmon | grep -v grep' | awk '{ print $8 }'</dev/null`
[oracle#proddb02]$ echo $DB_STAT
[oracle#proddb02]$ echo $DB_STAT
The loop code is
[oracle#proddb02]$cat test.sh
while read line
do
INST_VAR=`echo $line | awk '{ print $1 }'`
HOST_VAR=`echo $line | awk '{ print $2 }'`
SERVER_NAME=$HOST_VAR
INSTANCE_NAME=$INST_VAR
DB_STAT=`ssh -X proaddb01 'ps -ef | grep pmon | grep -v grep' | awk '{ print $8 }'`
echo $DB_STAT
done < host_list.lst
Any help would be much appreciated.
Just do:
DB_STAT=`ssh -X proaddb01 "ps -ef | awk '/[p]mon/'{ print $8 }"`
Here you use:
// to filter the input stream (like grep but witj awk);
the trick with [p]grep to exclude the same line from ps (because [p]grep string will not be found with [p]grep template).
Related
I am trying to get the total disk usage of my machine. Below is the script code:
#!/bin/sh
totalUsage=0
diskUse(){
df -H | grep -vE '^Filesystem|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
diskUsage=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
totalUsage=$((totalUsage+diskUsage))
done
}
diskUse
echo $totalUsage
While totalUsage is a global variable, I have tried to sum the individual disk usage to totalUsage in the line:
totalUsage=$((totalUsage+diskUsage))
An echo of totalUsage between do and done shows the correct value,
but when I try to echo it after my call diskUse, it stills prints a 0
Can you please help me, what is wrong here?
The variable totalUsage in a sub-shell doesn't change the value in the parent shell.
Since you tagged bash, you can use here string to modify your loop:
#!/bin/bash
totalUsage=0
diskUse(){
while read output;
do
diskUsage=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
totalUsage=$((totalUsage+diskUsage))
done <<<"$(df -H | grep -vE '^Filesystem|cdrom' | awk '{ print $5 " " $1 }')"
}
diskUse
echo $totalUsage
I suggest to insert
shopt -s lastpipe
as new line after
#!/bin/bash
From man bash:
lastpipe: If set, and job control is not active, the shell runs the last command of a pipeline not executed in the background in the current shell environment.
I just saw a code with a check in if as
for x in `ps -ef | awk '{ print $2 }'`
do
if [ "$x" != "PID" ];then
----- do something -----
fi
done
May I know why do we need to have this check in if and what is it doing ?
That check is added to skip the header part. you can remove whole if statement by modifying awk statement to skip first line. To find out what header is run ps -eaf |head -n 1 .
for x in $(ps -ef | awk 'NR>1{ print $2 }')
do
--------Do something with $x---------
done
If you run ps -ef you will notice, that it will print a header:
UID PID PPID TTY STIME COMMAND
[...]
the check skips the header.
ps -ef | awk '{ print $2 }' outputs' as below; the first line is PID, so if statetement is used exclude PID line;
user#host:/tmp$ ps -ef | awk '{ print $2 }' | head
PID
1
2
3
5
7
8
9
10
you can also use this without if;
#!/bin/bash
for x in `ps -ef | awk '{ print $2 }' | grep -v PID`
do
#----- do something -----
echo $x
done
This follows on from Faulty tail syntax or grep command? but I'm reading a live log entries for given conditions and when they're met continuing the execution of the rest of the script. I'm using this:
tail -Fn0 /var/log/messages | grep -q "CPU utilization" | grep -q "exceeded threshold"
FPC=$(echo $line | awk 'END { print substr($8,1,1) }')
PIC=$(echo $line | awk 'END { print substr($11,1,1) }')
echo FPC $FPC
echo PIC $PIC
echo "Running information gathering"...and rest of script.
Which works perfectly for the conditions detection and further execution, but I don't have the log entry to test for the FPC and PIC variables. I've tried wrapping the tail statement thus:
line=$(tail -Fn0 /var/log/messages | grep -q "CPU utilization" | grep -q "exceeded threshold")
but grep -q exits silently and the $line variable is blank. I've tried:
line=$(tail -Fn0 /var/log/messages | grep -m1 "CPU utilization" | grep -m1 "exceeded threshold")
which doesn't work until I attempt to CONTROL-C out of the script. Then it works fine and continues perfectly. Can someone help please?
I need the variables FPC and PIC later in the script.
Assuming that you don't need these variables later on, you could do something like this:
tail -Fn0 /var/log/messages | \
awk '/CPU utilization/ && /exceeded threshold/ {
print "FPC", substr($8,1,1); print "PIC", substr($11,1,1); exit }'
When the line matches both patterns, print the two parts of it that you are interested in and exit.
If you do need the variables, you could do something like this instead:
line=$(tail -Fn0 /var/log/messages | awk '/CPU utilization/&&/exceeded threshold/{print;exit}')
FPC=$(echo "$line" | awk '{ print substr($8,1,1) }')
PIC=$(echo "$line" | awk '{ print substr($11,1,1) }')
I am trying to write a script to find a reverse SSH PID and kill it if present. I am stuck on "awk" as it gives error. below is the script:
a=('ps -aef | grep "ssh -fN" | grep -v grep | awk '{ print $2 }'')
if [ -n "$a" ]
then
echo "String \"$a\" is not null."
kill -9 "$a"
fi
I commented out if, then, kill and fi lines to debug the script. I get following error:
String "ps -aef | grep "ssh -fN" | grep -v grep | awk {" is not null.
I believe parenthesis for awk is creating the problem and I am unable to get a workaround for this. On Command line, this works perfectly and returns the correct PID.
ps -aef | grep "ssh -fN" | grep -v grep | (awk '{ print $2 }'
Once the PID is passed on to variable "a", I need to issue kill command. OS is Centos 6.4
P.S: I am not fluent on scripting but trying to achieve an objective. Help will be highly appreciated!
There are multiple problems with your script.
You need command substitution to store the output of ps pipeline into an array.
You need to check for the number of elements in the array.
Refer to the array instead of the variable.
The following might work for you:
pids=( $(ps -ef | grep '[s]sh -fN' | awk '{print $2}') )
if [ "${#pids[#]}" -gt 0 ]; then
kill -9 "${pids[#]}";
fi
First, if you have grep and then awk, you can get rid of the greps:
ps -aef | grep "ssh -fN" | grep -v grep | awk '{ print $2 }'
ps -aef |awk ' { if ( ($0 ~ /ssh -FN/) && (! $0 ~ /grep/) ) { print $2 } }'
However, instead of using ps, use pgrep.
pgrep -f "ssh -[fN][fN]" # Will match against either 'ssh -fN' or 'ssh -Nf'
There is even a pkill that will do the entire command for you:
pkill -f "ssh -[fN][fN]"
That will find all of the processes that match that particular string and kill them (if they exist).
My bash script is:
output=$(curl -s http://www.espncricinfo.com/england-v-south-africa-2012/engine/current/match/534225.html | sed -nr 's/.*<title>(.*?)<\/title>.*/\1/p')
score=echo"$output" | awk '{print $1}'
echo $score
The above script prints just a newline in my console whereas my required output is
$ curl -s http://www.espncricinfo.com/england-v-south-africa-2012/engine/current/match/534225.html | sed -nr 's/.*<title>(.*
?)<\/title>.*/\1/p' | awk '{print $1}'
SA
So, why am I not getting the output from my bash script whereas it works fine in terminal am I using echo"$output" in the wrong way.
#!/bin/bash
output=$(curl -s http://www.espncricinfo.com/england-v-south-africa-2012/engine/current/match/534225.html | sed -nr 's/.*<title>(.*?)<\/title>.*/\1/p')
score=$( echo "$output" | awk '{ print $1 }' )
echo "$score"
Score variable was probably empty, since your syntax was wrong.