- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: 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
Discussions
Discussions
Forums
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
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.