Kill a process in NT 4.0

I've used "kill" & "psKill" & Windows XPs "taskkill" but I'm not at a solution yet....

I have an NT 4.0 box that I want to kill a specific task on.  BUT....I need to specify the username running the task.  This is on an NT 4.0 server.
If I want to kill "cmd.exe" (for instance) I need to specify the user running it, because I may have 6 users with that task open at a given moment.

I can do this with "Taskkill" (in XP) but I read it does not work with Windows NT 4.0.
...for instance, with taskkill I'd type: taskkill /F /FI "username eq johnsmith" /IM cmd.exe
This would kill the cmd.exe task running under user johnsmith and leave the other cmd.exe processes intact.

Is there a way I can stop a few specific tasks in NT 4.0 that makes use of the username?  I cannot use the PID - that will not work for me.

-z-
LVL 15
ZabagaRAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gheistCommented:
Go to www.sysinternals.com and get process explorer, it has command line equivalents too
ZabagaRAuthor Commented:
I have process explorer already.  If you know how to use it, to achieve my desired result, explain away.
I don't think it will.

 
gheistCommented:
there is kill.exe on resource kit CD, i left windows world long ago..
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

oBdACommented:
Why not use the PID? You can use pulist.exe from the W2k Resource Kit (runs fine on NT4) to retrieve the PID of the process run by the given user, then kill it.
This script will do already most of the work; you just have to add your favorite command to kill the process using the PID.

Pulist.exe
http://www.microsoft.com/windows2000/techinfo/reskit/tools/existing/pulist-o.asp

====8<----[KillPerUser.cmd]----
@echo off
setlocal
if "%2"=="" goto Syntax
set Process=%1
set User=%2
set PID=
for /f "tokens=1-4 delims=\ " %%a in ('pulist ^| find /i "%Process%"') do (
  if /i "%%d"=="%User%" (
    set PID=%%b
  )
)
if "%PID%"=="" (
  echo Process "%Process%" from User %User% not found.
  goto :eof
)
echo Process "%Process%" found, PID is %PID%.
echo Killing %PID% ...
:: *** Use your favorite kill tool here with the PID:

goto :eof
:Syntax
echo.
echo KillPerUser.cmd
echo.
echo Kills a process started by a given user.
echo Syntax: killperuser ^<process^> ^<User^>
echo.
====8<----[KillPerUser.cmd]----
ZabagaRAuthor Commented:
oBda - I appreciate your response.....but in my original question, did you see the part that read, "I cannot use the PID - that will not work for me" ???

Did you see my "taskkill" example for WinXp....It just requires that I input the process I want to whack, and the user name executing it.  That's it.  I'm looking for the NT 4.0 equivalent!!!

It cannot involve any user interaction.  I'm going to make a batch file and give it to a non-technical user.  The batch file is going to kill the processes necessary.  So that is a major reason why this has to involve nothing more than clicking an icon which in turn will call my kill command.


oBdACommented:
Well, there's no functional difference between killing a process by name or by PID. Actually, the process is only ever killed by PID, as the name (as you've noticed) isn't neessarily unique; the PID is.
Here's an enhanced version; if you're really looking to kill a cmd.exe, and since this is a batch file, this version takes care not to kill itself.
For this, it uses tlist.exe from the W2k Support Tools (runs on NT4 as well). To kill, it uses kill.exe, but you can replace that with any other tool that kills a process by PID.
You can then run this script by starting
killperuser.cmd cmd.exe johnsmith
and it will kill only cmd.exe started by johnsmith.
Yes, the list of necessary programs is somewhat extensive, but it does the job (pulist.exe lists the user name, but only the generic process name; tlist.exe doesn't list the user, but the window title ...)

Windows 2000 SP4 Support Tools
http://www.microsoft.com/windows2000/downloads/servicepacks/SP4/supporttools.asp


@echo off
setlocal
if "%2"=="" goto Syntax
set MyTitle=KillPerUser-Running
set TempFile=%Temp%\killperuser.tmp
title %MyTitle%
set Process=%1
set User=%2
set PID=
for /f "tokens=1" %%a in ('tlist ^| find /i "%MyTitle%"') do set MyPID=%%a
echo Own PID: %MyPID%
pulist >"%TempFile%"
for /f "tokens=1-4 delims=\ " %%a in ('type "%TempFile%"') do (
  if not "%%b"=="%MyPID%" (
    if /i "%%a"=="%Process%" (
      if /i "%%d"=="%User%" (
        set PID=%%b
      )
    )
  )
)
del "%TempFile%"
if "%PID%"=="" (
  echo Process "%Process%" from User %User% not found.
  goto :eof
)
echo Process "%Process%" found, PID is %PID%.
echo Killing %PID% ...
:: *** Use your favorite kill toll here with the PID:
kill -f %PID%

goto :eof
:Syntax
echo.
echo KillPerUser.cmd
echo.
echo Kills a process started by a given user.
echo Syntax: killperuser ^<process^> ^<User^>
echo.
ZabagaRAuthor Commented:
I'll take a look at this later when I'm back at work, but it seems like an awful lot compared to the 1 line solution from XP (alas, it won't run in NT 4.0)....I don't want to over-engineer the answer. It also looks like its still PID dependent too.

I don't completely follow your program.....but it could be because my brain is currently fried.  What files am I suppposed to download?   I already have all the SP4 support tools handy.

Here is why I do not care about the PID and only need the process name[s] and username:

I have an application that uses user account "joeblow" to run 3 processes when the computer boots up.
Every time it boots, the PID is completely different for those 3 processes, so I can't use it.  But, slappy, the 3 distinct processes and the user that initiated them are the same 100% of the time: thus I need to stop the processes based on their name and their creator.  Other users will also be running those 3 same processes, so I need to single out the ones only started by "joeblow".  I need to have a batch file that some layperson will click on to stop those processes when needed. So, PID is out.  

okay, I'm going....too tired & irritated....plus I'm at home and this is work.....am i crazy?

-z-



oBdACommented:
Well, the solution above is an "all purpose" solution. And, yes, it does use the PID, but the PID is dynamically retrieved from the actual process name.
For your specific scenario, the whole thing can be boiled down:

@echo off
setlocal
:: *** the user name as it appears in the pulist.exe output:
set ProcessUser=SomeDomain\joeblow
:: *** A list of processes to kill when started by above user:
set ProcessList=calc.exe notepad.exe
:: *** A temporary file, will be deleted after use:
set TempFile=%Temp%\killperuser.tmp

pulist | find /i "%ProcessUser%" >"%TempFile%"
for /f "tokens=1,2" %%a in ('type "%TempFile%"') do (
  for %%p in (%ProcessList%) do (
    if /i "%%p"=="%%a" ECHO kill -f %%b
  )
)
del %TempFile%

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZabagaRAuthor Commented:
So do I need any add-on .exe utils from sysinternals, the resource kit, etc?  
You sure seem to like cryptic answers.

-z-
ZabagaRAuthor Commented:
.....let me ammend my response....looks like I need "pulist" and "kill" .....is that right? anything else I'm missing?
oBdACommented:
Oops; yes, should indeed have mentioned this. You're right, this version only needs pulist.exe and kill.exe; instead of kill.exe, you could use another one as well. Just haven't found any other tool to display a process' user information.
ZabagaRAuthor Commented:
Well I'll try it when I have the opportunity;  which could be tomorrow or a few days (the NT 4.0 system is at a remote site to me).  Hey, if it works, my many thanks + points....plus you had to put up with me being irritated.... :-)

-z-
oBdACommented:
Just in case (it's rather obvious, but sometimes things are too obvious), note that for testing purposes, the kill command will currently only be displayed, not run. Remove the capitalized "ECHO" in front of the kill command to actually run the script.
ZabagaRAuthor Commented:
he he he........
I ran it then realized it didn't work because it only echoed the kill to the screen. So I removed it & it all worked well.
Then I signed on here and read your note, saying to remove the echo.  Why'd you put it there?

Well, anyway, I'm thrilled that it all worked and I thank you a million times.
oBdACommented:
Just makes for easier testing; that way you don't have to start the processes again while checking if it's working properly. It's especially useful if you're writing a script that deletes files ...
Glad you got it to work after all--even with the PID :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Operating Systems

From novice to tech pro — start learning today.