- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Script to calculate the total idle minutes
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2007 06:02 PM
02-20-2007 06:02 PM
ivcheung pts/th .
ivcheung pts/tp .
ktam pts/tx 0:02
kwchow pts/tr 0:54
mmak pts/tk 1:16
ritali pts/tc 0:01
rwong pts/tt 0:08
tchan pts/tw 0:15
Then find out those telnet sessions that have been idled for more than 30 minutes
My script is like :
num=`grep pts $tempdir/whodo.out | wc | cut -f1 -d' '`
lines=1
while (( num > 0 ))
do
info=`head -n $lines $tempdir/whodo.out | tail -1`
userid=`echo $info | cut -f1 -d' '`
tty=`echo $info | cut -f2 -d' '`
idle=`echo $info | cut -f3 -d' '`
if [ $idle = "old" ]
then
idle_min=999999
else
(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':'
) ))
fi
if [ $idle_min -gt 30 ]
then
tty="/dev/"$tty
( invoke another script to kill the idled session )
fi
(( lines = lines + 1 ))
(( num = num - 1 ))
done
This script used to work fine until recently I moved from a 11.0 server to another 11.i server, if the idled time = "0:08" or "0:09", error message would occur
08 : The specified number is not valid for this command.
I know I can use
echo $(( 10#08 ))
to force a base 10 echo but don't know how to embed this into the old lines
Rita
Solved! Go to Solution.
- Tags:
- idle
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2007 06:32 PM
02-20-2007 06:32 PM
Re: Script to calculate the total idle minutes
typeset -LZ idle=$(echo $info | cut -f3 -d' ')
>num=`grep pts $tempdir/whodo.out | wc | cut -f1 -d' '`
Why do you need the cut? wc -l will give lines directly. Also, you can use grep -c, so you don't need wc.
Any reason you use num and a while loop and getting the lines one at a time with: info=`head -n $lines $tempdir/whodo.out | tail -1`
Why not use awk to process the whole file until EOF?
By using awk, you can make it lots simpler to read. And no stinkin' problems with garbage "octal" leading zeroes. And you don't need to use cut. You would have to use substr for the ":" extraction of the MM:SS time.
If you aren't familiar with awk, you could at least use:
grep pts $tempdir/whodo.out |
while read userid tty idle; do
...
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2007 07:21 PM
02-20-2007 07:21 PM
Re: Script to calculate the total idle minutes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2007 07:58 PM
02-20-2007 07:58 PM
Solutionyou could try:
idle_hr=`echo ${idle} |cut -f1 -d':'`
idle_min=`echo ${idle} |cut -f2 -d':'`
idle_total=`echo "$idle_hr * 60 + $idle_min"|bc`
instead of your:
(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':'
) ))
- Tags:
- bc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2007 08:18 PM
02-20-2007 08:18 PM
Re: Script to calculate the total idle minutes
typeset -LZ idle_min=$(echo $info | cut -f3 -d' ')
>My problem is: I am not familar with awk
Then use my "grep | while read userid tty idle; do" suggestion.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2007 08:20 PM
02-20-2007 08:20 PM
Re: Script to calculate the total idle minutes
Oops that's not your initial value. Just add this before your code:
typeset -LZ idle_min
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2007 08:40 PM
02-20-2007 08:40 PM
Re: Script to calculate the total idle minutes
instead of this line:
(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':') ))
try this:
(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo ${idle#[0-9]*:0}|cut -f2 -d':'
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2007 08:53 PM
02-20-2007 08:53 PM
Re: Script to calculate the total idle minutes
In fact, I tested your script in my 11.11 box and it runs fine.
Which shell is using in 11.11 ?
Have you tried several shells (ksh, sh-posix) ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2007 09:08 PM
02-20-2007 09:08 PM
Re: Script to calculate the total idle minutes
Perhaps UNIX95 was set?
It is hard to argue when Rita had the error message:
08 : The specified number is not valid for this command.
Of course it would be nice to know which command. I have no problems duplicating it on 11.23:
./itrc_leadzero.sh[26]: 08 : The specified number is not valid for this command.
26 (( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':') ))
Which means you need my: typeset -LZ idle_min
With Peter's extra temps:
idle_hr=`echo ${idle} |cut -f1 -d':'`
idle_min=`echo ${idle} |cut -f2 -d':'`
idle_total=(( idle_hr * 60 + idle_min ))
Of course bc(1) probably doesn't have this scummy C octal rule.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2007 09:18 PM
02-20-2007 09:18 PM
Re: Script to calculate the total idle minutes
Hi Dennis
I agree Denis when he wrote that the problem might be UNIX95.
According to sh-posix man pages:
If UNIX95 is defined, shell arithmetic evaluators let command,
$((...)) and ((...)) recognizes interger constants beginning with 0
and 0x (or 0X) as octal and hexadecimal numbers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2007 09:29 PM
02-20-2007 09:29 PM
Re: Script to calculate the total idle minutes
(( idle_min = 10#$(echo $idle|cut -f1 -d':') * 60 + 10#$(echo $idle|cut -f2 -d':') ))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2007 01:20 AM
02-21-2007 01:20 AM
Re: Script to calculate the total idle minutes
Please consider the following free advice, worth every penny.
1) This particular wheel has been invented many times over. Please look around for existing commercial or freeware solutions.
It looks simple enough now, but it is probably too simple. You'll soon need exclusion lists and time ranges and stuff like that.
2) Every Unix sysadm MUST try to aquire some awk and perl skills. Not saying you should start doing everything with those (allthough you could :-), but for your own career you MUST get going on those powerful tools.
3) Idle killers suck... IMHO. They suck computer resources, they suck positive energy out of users, they suck at providing security, they suck admin time. They do exactly waht their name implies, but not what you want: They kill idleness and replace it with busyness. Sounds like a bad virus to me!
4) Consider replacing the "ge" in you script with a simple ">". The leading zero minute field compares nicely as a string instead of numbers.
5) Just to trigger your cusiosity consider this perl script reading the smaple input your gave:
#perl -lne 'if (/(\d+):(\d\d)$/) { ($u,$t,$i)=split; print qq($u on $t idle for $i) if ($1>0 or $2>30)}' tmp.txt
kwchow on pts/tr idle for 0:54
mmak on pts/tk idle for 1:16
#perl -lne -e = cmmand follows, -l=print newlines, -n=loop throug input Not printing.
if (/(\d+):(\d\d)$/){ = Look for decimals, a colong and two decimals at the end of the line.
If found remember in $1 and $2 and execute block.
($u,$t,$i)=split; # AAyup that's what it does.
print qq($u on $t idle for $i) if # conditional statement
($1>0 or $2>30)} # The hour not 0, or minutes greate than 30
Cheers,
Hein.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2007 03:44 AM
02-21-2007 03:44 AM
Re: Script to calculate the total idle minutes
(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':'
) ))
with...
(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':' | bc
) ))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2007 03:53 AM
02-21-2007 03:53 AM
Re: Script to calculate the total idle minutes
please see my bc solution posted at 08:58:06
;-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2007 06:40 AM
02-21-2007 06:40 AM
Re: Script to calculate the total idle minutes
On 11.23, this UNIX95 has been removed. In all cases it:
... will be processed according to ISOC standard
- Tags:
- UNIX95
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2007 05:50 PM
02-22-2007 05:50 PM
Re: Script to calculate the total idle minutes
I found more than 1 solution, but I choose the most understanable one, the "|bc", also rewrote this script which is written few years ago in a more structured format
Correct, I need to acquire to learn some awk knowledge & someone else in my team is learning Perl, so may rewrite this script in Perl later
Thanks,
Rita