Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

uptime check

Highlighted
tempsample
Frequent Advisor

uptime check

Hi ,

 

 

>>uptime
  7:40pm  up 121 days, 14:47,  1 user,  load average: 0.37, 0.30, 0.30

>>uptime | awk -F, '{sub(".*up ",x,$1);print $1,$2}'
121 days  14:47

Can you please tell me,How the awk command is working for above uptime.

 

 

 

5 REPLIES
Dennis Handly
Acclaimed Contributor

Re: uptime check, awk sub function

>How the awk command is working?

>awk -F, '{sub(".*up ", x, $1); print $1, $2}'

 

This says split fields based on comma: $1="7:40pm  up 121 days" $2=" 14:47" $3=" 1 user" ...

Then is uses the sub function on $1 to replace an ERE with the first part of the string, up to "up ".

It replaces it by the variable "x" which is initialized to an empty string.

A better command would be to use a string constant: sub(".*up ", "", $1)

tempsample
Frequent Advisor

Re: uptime check, awk sub function

Hi Dennis,

 

Thanks for suggestion.

 

   for HOST in $(< /tmp/hostdetails.txt); do
   echo
   ssh -q $HOST uptime | awk -F, '{sub(".*up", "", $1);print $1,$2}'
   done

 

 

I am writing a script which checks the uptime of server.

 

I am running the script from a centralized server.

 

I need all uptime logs from other  server to be availble in centralized server.

 

How can I do it ?

 

above script displays only uptime of all server,but I have to collect all uptime logs in centralized server.

Dennis Handly
Acclaimed Contributor

Re: uptime check, awk sub function

>I need all uptime logs from other server to be available in centralized server.

 

If you run the script on your central server, you can simply redirect stdout:

   echo $HOST

...

done >> uptime.log

 

This will append any stdout output in the loop to uptime.log.

 

 

tempsample
Frequent Advisor

Re: uptime check, awk sub function

Hi ,

I have two points to be clarified.

 

First Point

 

 

I am running the script from solaris server to check uptime status of hp-ux server.

 

>>for HOST in $(< /tmp/hostdetails.txt); do

 

when I use above format,I am getting error : syntax error $ unexpected.

 

 

but it is running fine with `cat /tmp/hostdetails.txt`

 

I need to know why i am getting syntax error for above cmd.

Second Point :

 

for example if a server is down or decommissioned,so when i run uptime script ,it gets waiting to get server status,but actually server is down.

 

I need to know how can i avoid this problem.

 

 

 

 

Dennis Handly
Acclaimed Contributor

Re: uptime check, awk sub function

>I am running the script from solaris server to check uptime status of HP-UX server.

 

Then your question doesn't belong in an HP-UX forum.

 

>when I use above format, I am getting error: syntax error $ unexpected.

 

I assume that Solaris has some real shells, ksh/Posix?  And not scummy csh/tcsh nor Bourne sh.

 

>for example if a server is down or decommissioned

 

See your other topic for setting ssh timeouts:

http://h30499.www3.hp.com/t5/Languages-and-Scripting/setting-timeout-value/m-p/6346969#M46743