Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 User inactivity time out

Author  Topic 

MarkLandau
Starting Member

6 Posts

Posted - 2012-02-09 : 10:28:15
I am limited on the amount of users licenses for my software.

I would like to find a method of detecting when a user is idle, shutting down the user session and giving them a message telling them session is terminated.

Can I use the Policy Management?

Is there a quick Script I can use?

Thank you for your help and time.

xx

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2012-02-09 : 13:47:00
Sounds like you're already violitaing your license agreement.

This will kill idle sessions:
declare @spid int
Declare @kill varchar (12)

create table #t (
spid int,
status varchar(64),
[login] varchar(128),
hostname varchar(128),
blkBy varchar(32),
dbname varchar(128),
command varchar(128),
cputime int,
diskio int,
lastbatch varchar(24),
programname varchar(128),
spid2 int,
requestId int
)

insert #t
exec sp_who2

Declare c Cursor
Read_Only
For
select spid
from #t
where spid > 50
and rtrim(status) <> 'BACKGROUND'
-- 3 hours. Change to whatever you want
And datediff(hour,
convert(smalldatetime, convert(char(4), year(getdate())) +
left(lastbatch, 2) +
substring(lastbatch, 4, 2) + ' ' +
right(lastbatch, 8)
),
getdate()
) > 3
--And login NOT IN (Make sure to exclude the service account, replication account etc. here)
open c
fetch next from c into @spid
while @@fetch_status = 0
begin
set @kill = 'KILL ' + convert(varchar(32), @spid)
Exec(@kill)
fetch next from c into @spid
end

close c
deallocate c

drop table #t
Go to Top of Page

MarkLandau
Starting Member

6 Posts

Posted - 2012-02-09 : 17:22:45
Thank you Russel I will try it.

xx
Go to Top of Page
   

- Advertisement -