Link to home
Start Free TrialLog in
Avatar of ZabagaR
ZabagaRFlag for United States of America

asked on

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-
Avatar of gheist
gheist
Flag of Belgium image

Go to www.sysinternals.com and get process explorer, it has command line equivalents too
Avatar of ZabagaR

ASKER

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.

 
there is kill.exe on resource kit CD, i left windows world long ago..
Avatar of oBdA
oBdA

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]----
Avatar of ZabagaR

ASKER

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.


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.
Avatar of ZabagaR

ASKER

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-



ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ZabagaR

ASKER

So do I need any add-on .exe utils from sysinternals, the resource kit, etc?  
You sure seem to like cryptic answers.

-z-
Avatar of ZabagaR

ASKER

.....let me ammend my response....looks like I need "pulist" and "kill" .....is that right? anything else I'm missing?
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.
Avatar of ZabagaR

ASKER

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-
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.
Avatar of ZabagaR

ASKER

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.
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 :-)