# How to stop/start a process as shown in Task Manager



## mcseguy9 (Jun 11, 2001)

Hello all. I have a friend who has a SBS 2003 server and there's a process, let's call it process.exe, that needs to be stopped and restarted as often as its handles count reaches 50,000. What I'm trying to do is a band-aid fix until we permanently resolve the issue at which time we won't need to stop/restart the service.

Anyway, if you go into Task Manager and add the Handles column you'll see that each process uses a certain number of handles. We have one process where the handles increases constantly. In a 17 hour period it went from 14,000 (which is high to begin with) to nearly 200,000! I stopped/restarted the process by stopping/restarting the service in services.msc and that started it back at zero. Of course it's doing the same thing as before where the handles count just keeps increasing, hence my desire to setup something so it will automatically stop/restart this process until we get the actual problem fixed. It's important that we don't just stop this service from running until we get it fixed because it's related to the anti-virus software on the server.

I did try setting up an alert in Performance Monitor and tried to have it run a batch file that would stop the service, but that didn't work because the process itself, process.exe, isn't the service name or something like that. In other words when I opened a command prompt and typed net stop process.exe I got an error message stating it wouldn't work because that's not the name of the service. Perhaps if I could determine the actual name of the service this could work?

So does anyone know how to setup something that will cause the process or service to stop and then restart every time the handles reaches 50,000? We could possibly just setup a task to run every X number of hours if we can't get this to work based on the number of handles being reached.


----------



## wedor (Nov 7, 1999)

Look through the services to determine the actual service running and then set-up a task to restart it at set intervals.

You already know the real fix is to get the software writer to fix this program.

What program is this? The last time I had an issue like this is with one of the anti-virus programs, I notified them of the issue and they sent me a fix for it.


----------



## TheOutcaste (Aug 8, 2007)

try *tasklist /FI "IMAGENAME eq process.exe" /SVC* and see if it will list the service name.
Then you can use *sc GetDisplayName "service_name"* to get the Display name you would see in Services.msc

If that doesn't work, try this little batch file. Replace the process name in red (netdde.exe) with the name of the process you are looking for.


```
@Echo Off
SetLocal EnableDelayedExpansion
Set _Pname=[COLOR=Red]netdde.exe[/COLOR]
For /F "Tokens=1* Delims=: " %%I In ('sc query ^|Findstr /I "SERVICE_NAME"') Do (
sc qc "%%J" |Findstr /I "BINARY_PATH_NAME"|Findstr /I "%_Pname%"
If !ErrorLevel!==0 (
For /F "Tokens=1* Delims==" %%A In ('sc GetDisplayName "%%J"') Do (Echo Service Name: "%%J")&(Echo DisplayName: "%%B")
))
```
Once you know the Service name or the Display name, you can use the following to check the handle count and restart the service if needed.
The value for *_SVCName* can be either the Service Name or the Display Name found by the above program, so in this example *NetDDEdsdm* or *Network DDE DSDM* would work. If the handle count is 50000 or grater, the service is stopped, it waits 10 seconds, then starts the service. You can adjust the wait time if needed by adjusting the -W parameter in the Ping statement, which is in milliseconds.

```
@Echo Off
Set _SVCName=[COLOR=Red]NetDDEdsdm[/COLOR]
Set _Pname=[COLOR=Red]netdde.exe
[/COLOR]Set _Handles=
For /F "Tokens=2 Delims==" %%I In ('wmic process WHERE Description^="%_Pname%" Get HandleCount /value') Do Set _Handles=%%I
Echo Handles for %_Pname% is : %_Handles%
If NOT Defined _Handles Goto :EOF
If %_Handles% lss 50000 Goto :EOF
Net Stop "%_SVCName%"
Ping 1.0.0.0 -n 1 -w 10000>Nul
Net Start "%_SVCName%"
Ping 1.0.0.0 -n 1 -w 10000>Nul
```
You'd schedule this last as a scheduled task to run every XX minutes to check the current handle count.

Though if you know which service it is in *Services.msc* you already know the service name to use with Net Stop, it's the same as is displayed in Services.msc


----------

