Unix Programming - Kill script(CCCL)

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2004 > Kill script(CCCL)





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Kill script(CCCL)
Shakthi

2004-10-19, 2:48 am

Dears,

Greetings to Unix/solaris Users..

I want one script to kill the login more than 60minities idle.
Example :
# who -u
gatepass pts/22 Oct 19 11:39 . 22216 (192.9.200.31)
root console Oct 9 18:47 0:25 639 (:0)
stores pts/4 Oct 19 08:43 2:02 19567 (192.9.200.31)
fas pts/2 Oct 19 10:11 0:04 20451 (192.9.200.21)
sis pts/5 Oct 19 08:24 . 19495 (192.9.200.12)
sis pts/25 Oct 19 11:47 0:37 22550 (192.9.200.16)
sis pts/7 Oct 19 08:36 0:08 19556 (192.9.200.11)
stores pts/8 Oct 19 08:44 0:03 19581 (192.9.200.31)
stores pts/11 Oct 19 08:44 2:00 19595 (192.9.200.31)
gatepass pts/12 Oct 19 08:44 1:33 19611 (192.9.200.31)
pur pts/13 Oct 19 08:58 1:33 19643 (192.9.200.31)
ccc pts/14 Oct 19 09:16 1:39 19774 (192.9.200.122)
stores pts/15 Oct 19 08:58 1:03 19668 (192.9.200.33)
sis pts/21 Oct 19 10:32 1:45 20927 (192.9.200.12)
ccc pts/28 Oct 19 11:50 . 22592 (192.9.200.81)
sis pts/20 Oct 19 09:28 3:00 19874 (192.9.200.11)
mms pts/18 Oct 19 11:07 1:51 21569 (192.9.200.64)
stores pts/27 Oct 19 10:18 2:52 20643 (192.9.200.33)
aps pts/32 Oct 19 12:25 . 23172 (192.9.200.24)
fas pts/33 Oct 19 10:11 0:04 20440 (192.9.200.21)
fas pts/34 Oct 19 10:11 . 20454 (192.9.200.21)
fas pts/35 Oct 19 10:11 . 20457 (192.9.200.21)
root pts/6 Oct 19 11:44 0:25 22495 (:0.0)
#

in the above example i want more than one hour idle..such as...

gatepass pts/12 Oct 19 08:44 1:33 19611 (192.9.200.31)
pur pts/13 Oct 19 08:58 1:33 19643 (192.9.200.31)
ccc pts/14 Oct 19 09:16 1:39 19774 (192.9.200.122)
stores pts/15 Oct 19 08:58 1:03 19668 (192.9.200.33)
sis pts/21 Oct 19 10:32 1:45 20927 (192.9.200.12)
stores pts/27 Oct 19 10:18 2:52 20643 (192.9.200.33)

Please give the script to me..

Regards
Shakthi
Dana French

2004-10-19, 5:51 pm

shakthi@chettinadmail.com (Shakthi) wrote in message news:<b7d27908.0410182331.7f7b7636@posting.google.com>...
> Dears,
>
> Greetings to Unix/solaris Users..
>
> I want one script to kill the login more than 60minities idle.
> Example :
> # who -u
> gatepass pts/22 Oct 19 11:39 . 22216 (192.9.200.31)
> root console Oct 9 18:47 0:25 639 (:0)
> stores pts/4 Oct 19 08:43 2:02 19567 (192.9.200.31)
> fas pts/2 Oct 19 10:11 0:04 20451 (192.9.200.21)

SNIP


A ksh93 Solution:

#!/usr/bin/ksh93
who -u | while read -- UNAME TTYNUM MON DOM TIME IDLE PID ADDR
do
(( ${IDLE%%:*} >= 1 )) && kill ${PID}
done


--
Dana French dfrench@mtxia.com
Mt Xia Technical Consulting Group http://www.mtxia.com
100% Spam Free Email http://www.ridmail.com
MicroEmacs http://uemacs.tripod.com
Korn Shell Web http://dfrench.tripod.com/kshweb.html
Michael Vilain

2004-10-19, 5:51 pm

In article <3fa52bda.0410190750.2e26e52@posting.google.com>,
dfrench@mtxia.com (Dana French) wrote:

> shakthi@chettinadmail.com (Shakthi) wrote in message
> news:<b7d27908.0410182331.7f7b7636@posting.google.com>...
> SNIP
>
>
> A ksh93 Solution:
>
> #!/usr/bin/ksh93
> who -u | while read -- UNAME TTYNUM MON DOM TIME IDLE PID ADDR
> do
> (( ${IDLE%%:*} >= 1 )) && kill ${PID}
> done


Alternately you could look at idled

http://www.darkwing.com/idled/

and LOGMON from Computronics: http://www.logmon.com/

--
DeeDee, don't press that button! DeeDee! NO! Dee...



Shakthi

2004-10-20, 2:47 am

Dear Sir

Thanks to your replay..

But it will say some errors in while area...

========================================
================
# cat ksh93
#!/usr/bin/ksh93
who -u | while read -- UNAME TTYNUM MON DOM TIME IDLE PID ADDR
do
(( ${IDLE%%:*} >= 1 )) && kill ${PID}
done

# sh ksh93
ksh93: --: is not an identifier
#

========================================
==================

How to rectify it..

Kindly give the solutions..

Regards
Shakthi

shakthi@chettinadmail.com (Shakthi) wrote in message news:<b7d27908.0410182331.7f7b7636@posting.google.com>...
> Dears,
>
> Greetings to Unix/solaris Users..
>
> I want one script to kill the login more than 60minities idle.
> Example :
> # who -u
> gatepass pts/22 Oct 19 11:39 . 22216 (192.9.200.31)
> root console Oct 9 18:47 0:25 639 (:0)
> stores pts/4 Oct 19 08:43 2:02 19567 (192.9.200.31)
> fas pts/2 Oct 19 10:11 0:04 20451 (192.9.200.21)
> sis pts/5 Oct 19 08:24 . 19495 (192.9.200.12)
> sis pts/25 Oct 19 11:47 0:37 22550 (192.9.200.16)
> sis pts/7 Oct 19 08:36 0:08 19556 (192.9.200.11)
> stores pts/8 Oct 19 08:44 0:03 19581 (192.9.200.31)
> stores pts/11 Oct 19 08:44 2:00 19595 (192.9.200.31)
> gatepass pts/12 Oct 19 08:44 1:33 19611 (192.9.200.31)
> pur pts/13 Oct 19 08:58 1:33 19643 (192.9.200.31)
> ccc pts/14 Oct 19 09:16 1:39 19774 (192.9.200.122)
> stores pts/15 Oct 19 08:58 1:03 19668 (192.9.200.33)
> sis pts/21 Oct 19 10:32 1:45 20927 (192.9.200.12)
> ccc pts/28 Oct 19 11:50 . 22592 (192.9.200.81)
> sis pts/20 Oct 19 09:28 3:00 19874 (192.9.200.11)
> mms pts/18 Oct 19 11:07 1:51 21569 (192.9.200.64)
> stores pts/27 Oct 19 10:18 2:52 20643 (192.9.200.33)
> aps pts/32 Oct 19 12:25 . 23172 (192.9.200.24)
> fas pts/33 Oct 19 10:11 0:04 20440 (192.9.200.21)
> fas pts/34 Oct 19 10:11 . 20454 (192.9.200.21)
> fas pts/35 Oct 19 10:11 . 20457 (192.9.200.21)
> root pts/6 Oct 19 11:44 0:25 22495 (:0.0)
> #
>
> in the above example i want more than one hour idle..such as...
>
> gatepass pts/12 Oct 19 08:44 1:33 19611 (192.9.200.31)
> pur pts/13 Oct 19 08:58 1:33 19643 (192.9.200.31)
> ccc pts/14 Oct 19 09:16 1:39 19774 (192.9.200.122)
> stores pts/15 Oct 19 08:58 1:03 19668 (192.9.200.33)
> sis pts/21 Oct 19 10:32 1:45 20927 (192.9.200.12)
> stores pts/27 Oct 19 10:18 2:52 20643 (192.9.200.33)
>
> Please give the script to me..
>
> Regards
> Shakthi

Chris F.A. Johnson

2004-10-20, 2:47 am

On 2004-10-20, Shakthi wrote:
> Dear Sir
>
> Thanks to your replay..
>
> But it will say some errors in while area...
>
> ========================================
================
> # cat ksh93
> #!/usr/bin/ksh93
> who -u | while read -- UNAME TTYNUM MON DOM TIME IDLE PID ADDR
> do
> (( ${IDLE%%:*} >= 1 )) && kill ${PID}
> done
>
> # sh ksh93
> ksh93: --: is not an identifier
> #


First, you are executing it with sh; that's a Bourne shell, I believe, on
Solaris. Don't use "sh ksh93", use:

../ksh93

Whatever sh is, it doesn't recognize '--' as meaning an end to
options. There's no point to it, anyway, as UNAME is a constant
not a variable. It would be a good idea (though it's not the cause
of your problem) to change the name to something that doesn't
collide with an executable in your path.


Second, the versions of ksh93 I have (Version M 1993-12-28 {m+,n})
do not recognize ">=" as an operator:

ksh: line 3: 00 \>= 1 : arithmetic syntax error

Use -ge instead.


Third, why not use a syntax that will work in all Bourne-type
shells as well as in ksh:

[ ${IDLE%%:*} -ge 1 ] && kill ${PID}


Fourth, I don't have a Solaris box here to check (and the output
of "who -u" differs from one OS to another), but are you sure that
${IDLE%%:*} will return a valid number?


Fifth, you will save yourself a lot of grief by using a case
statement; then it doesn't matter if $IDLE is not a number:

who -u | while read UNAME TTYNUM MON DOM TIME IDLE PID ADDR
do
case $IDLE in
[0-9][0-9]:*) echo $PID ;; ## check it first ## kill ${PID} ;;
esac
done


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Dana French

2004-10-20, 5:50 pm

"Chris F.A. Johnson" <cfajohnson@gmail.com> wrote in message news:<2tm7iuF20i4kaU1@uni-berlin.de>...
> On 2004-10-20, Shakthi wrote:
>
> First, you are executing it with sh; that's a Bourne shell, I believe, on
> Solaris. Don't use "sh ksh93", use:
>
> ./ksh93
>
> Whatever sh is, it doesn't recognize '--' as meaning an end to
> options. There's no point to it, anyway, as UNAME is a constant
> not a variable. It would be a good idea (though it's not the cause
> of your problem) to change the name to something that doesn't
> collide with an executable in your path.


Unclear what you mean by UNAME is a constant not a variable. If UNAME
is
set as a read-only variable on your system, then it is specific to
your
system and is non-standard.

>
>
> Second, the versions of ksh93 I have (Version M 1993-12-28 {m+,n})
> do not recognize ">=" as an operator:
>
> ksh: line 3: 00 \>= 1 : arithmetic syntax error
>
> Use -ge instead.
>


Thats odd, I am running the ATT binary version "Version M 1993-12-28
p" and it is recognized. I also tested it with the IBM implementation
of ksh93 "Version M-12/28/93e" and it works correctly. Regardless the
"-ge" operator is obsolete and should not be used.

>
> Third, why not use a syntax that will work in all Bourne-type
> shells as well as in ksh:
>
> [ ${IDLE%%:*} -ge 1 ] && kill ${PID}
>


Because the point of me participating in this newsgroup is to provide
ksh93 solutions.

>
> Fourth, I don't have a Solaris box here to check (and the output
> of "who -u" differs from one OS to another), but are you sure that
> ${IDLE%%:*} will return a valid number?
>


I based the solution on the data provided by the OP, if the OP
provided valid data, it should provide a valid number.

>
> Fifth, you will save yourself a lot of grief by using a case
> statement; then it doesn't matter if $IDLE is not a number:
>
> who -u | while read UNAME TTYNUM MON DOM TIME IDLE PID ADDR
> do
> case $IDLE in
> [0-9][0-9]:*) echo $PID ;; ## check it first ## kill ${PID} ;;
> esac
> done


That will certainly work, however the pattern you suggested will not,
The pattern needs to change to match any single or double digit number
equal to or greater than 1.

So I'll throw the ball back to you, in Korn/Bourne shell syntax,
represent a single or double digit number equal to or greater than 1.
Recognize that the pattern will need to recognize the numbers 0 and
10, and distinguish between them.


--
Dana French dfrench@mtxia.com
Mt Xia Technical Consulting Group http://www.mtxia.com
100% Spam Free Email http://www.ridmail.com
MicroEmacs http://uemacs.tripod.com
Korn Shell Web http://dfrench.tripod.com/kshweb.html
Dana French

2004-10-20, 5:50 pm

"Chris F.A. Johnson" <cfajohnson@gmail.com> wrote in message news:<2tm7iuF20i4kaU1@uni-berlin.de>...
>

SNIP
> Second, the versions of ksh93 I have (Version M 1993-12-28 {m+,n})
> do not recognize ">=" as an operator:
>
> ksh: line 3: 00 \>= 1 : arithmetic syntax error
>

SNIP

I think I figured out the problem there, the statement was:

(( ${IDLE%%:*} >= 1 )) && kill ${PID}

The "who -u" as represented by the OP will display a dot "." for the
current logins instead of the idle time. In the latest versions of
ksh93, a math statement will accept a dot "." on the left side of the
equation, earlier versions will not. So the statement should change
as follows:

(( ${IDLE//@(.|:*)/.0} >= 1 )) && kill ${PID}

This will substitute a dot or everything after the colon with a ".0"
and the math comparison should work correctly. The problem was not
the ">=" operator, rather it was the dot on the left side of the
comparison.

--
Dana French dfrench@mtxia.com
Mt Xia Technical Consulting Group http://www.mtxia.com
100% Spam Free Email http://www.ridmail.com
MicroEmacs http://uemacs.tripod.com
Korn Shell Web http://dfrench.tripod.com/kshweb.html
Paul Jarc

2004-10-20, 5:50 pm

dfrench@mtxia.com (Dana French) wrote:
> "Chris F.A. Johnson" <cfajohnson@gmail.com> wrote in message news:<2tm7iuF20i4kaU1@uni-berlin.de>...
>
> Unclear what you mean by UNAME is a constant not a variable.


I believe Chris is talking about the difference between these:
read UNAME
read "$varname"
In the first case, it's immediately obvious that there's no danger of
the variable name being interpreted as an option, so there's no need
to protect it with --.

>
> Because the point of me participating in this newsgroup is to provide
> ksh93 solutions.


In this case, it's easy to make the solution work in other shells as
well, without losing anything.

> So I'll throw the ball back to you, in Korn/Bourne shell syntax,
> represent a single or double digit number equal to or greater than 1.


Asssuming there will not be leading zeros:
case $num in
[2-9]|[1-9][0-9]*) echo greater than 1;;
*) echo not greater than 1 - possibly non-numeric;;
esac


paul
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com