New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
Correctly grep and display the uptime, load average and amount of users?
Because of the new version of my status monitoring script I've been busy rewriting the client script a little bit. See here
Now I find it quite hard to reliably parse the output of the uptime
command with only bash/sed/awk etc. I have a solution which works on my tests (iMac, Debian 6, RHEL 5). Maybe peoples can test it to see if they also get the correct output?
https://raymii.org/cms/p_Get_uptime_load_and_users_with_grep_sed_and_awk
Comments
@Raymii:
I prefer what you are doing (parsing
uptime
), but if you are having problems making it "cross-distro", you can try to get the info directly. I wrote these line on my machine only, but in theory they should work on all.You should be able to make my lines better with your
awk
skills.Only with awk ones tested on Linux and MAC
uptime: uptime | awk '{sub(/\,/,"");print $3" "$4 }'
users: uptime | awk '{gsub(/\,/,"");print $6" "$7 }'
load: uptime | awk '{gsub(/\,/,"");print $10 }'
Awk by default doesn't care too much of the number of spaces separating entries
@bnmkl the /proc filesystem is not available on every system I'm using.
@corehosting the amount of variables differs if a host is up less then a day.
True... Don't have anything with less than a day to test that out
Reboot like a sir! Also that is mostly fixed with an extra [if...fi]...
root@sg:~# uptime | awk '{sub(/\,/,"");print $3" "$4 }'
2:20 1
@William what do the commands I use give?:
And, what OS?
[root@curse ~]# uptime | awk '{sub(/\,/,"");print $3" "$4 }'
1:06 1
[root@curse ~]# uptime | grep -ohe 'up .' | sed 's/,//g' | awk '{ print $2" "$3 }'
1:06 1
[root@curse ~]# uptime | grep -ohe '[0-9.] user[s,]'
1 user,
[root@curse ~]# uptime | grep -ohe 'load average[s:][: ].*' | awk '{ print $3 }'
0.00,
EL6.3
@Corehosting thank you. CentOS/SL or real RHEL? My test on RHEL 6.3 gives me funky output after a reboot. Hope it is different tomorrow.
@Raymii: SL to be precise, but the result should be the same
Which version of procps ? So that we can compare.
Does that mean you are using some systems that have an alternate
uptime
command @Raymii? If so, then you must already have to test which system you are on to determine which command to use. Thus, I would still use/proc
to cover "cross-distro" for the ones that do as a function, then however many (least) other functions to cover the "system-sphere".The last commands you gave did not work correctly. My
uptime
format is:It does seem proven from your own words really that it can not be achieved by just passing
uptime
as it changes too much. Though I like your determination to keep trying! It is nice to have precise code, and sometimes, the code is more admirable than the application.