Automation Question: How do you stop a process from a .bat file?

  • Thread starter Thread starter mickey
  • Start date Start date
M

mickey

Problem: An application I run utilizes a service (supplied with the
application) to connect to a special USB device. When the application
terminates it leaves the service running. When the application is run again
it fails to connect to it's hardware. I have found that if I manually
terminate the service, an then run the application, the application re-starts
the service and always connects to the hardware. I have advised the
manufacturer of the issue, but as a temporary work-around I would like to
launch the application using a .bat file, and when the application
terminates, the .bat file will stop the service.

I have created a .bat file that successfully launches the application,
however I can't seem to stop the service.

The command I'm using is NET STOP service, but no matter how I reference the
service I get an error. The name of the service is XNETS.exe

Here's what I've tried:
NET STOP XNETS.exe; error can find service

I went to "Services" and copied the exact path listed in the properties for
the service.
NET STOP C:\Progra~1\Common~1\XNET\XNETS.exe; error in filename, path, etc.

I can manually stop this service using the task manager; I see it listed in
"Services" and can either stop or start it there; but I can't get the NET
STOP command to find it.

I would appreciate some suggestions, thanks.
 
I did a little research on Miscrosoft's Technet for scripting, and created a
script file that can terminate the service and launch the application.

There is one minor problem that I don't understand. The subject USB
hardware is connected through a USB Network HUB (Belkin). The software with
the Belkin Hub allows the option to automatically launch the application when
the hardware connects, and automatically disconnect the hardware when the
related application terminates. Again, because I need to terminate that
problematic service before the application is started, I'm using the script
file in to Belkin software that gets executed when the hardware connects.
The Belkin software is setup to disconnect the hardware when the application,
or in this case the script file terminates. Here's the problem, even though
I have a Do-Loop that waits for the application to terminate in the script
file, for some reason the Belkin software thinks the script file has
terminated as soon as it's started, and disconnects the hardware prematurely.
I even tried putting a "MsgBox" instruction at the very end of the script
file to verify that the loop is indeed working, but the Belkin software still
disconnects the hardware, before the application ends and the "MsgBox"
command executes.

Does anyone have an idea as to why the Belkin software thinks the script
file has ended when it hasn't, or is it possible that because it's a script
file and not an actual application there is no running flag?
 
mickey laid this down on his screen :
I have created a .bat file that successfully launches the application,
however I can't seem to stop the service.

The command I'm using is NET STOP service, but no matter how I reference the
service I get an error. The name of the service is XNETS.exe

Here's what I've tried:
NET STOP XNETS.exe; error can find service

XNETS.exe is the name of the executable - not necessarily the service
name. The service name is shown in the list of services; right-click on
a service and click properties, then the service name (and it's display
name) is shown. If the service name has blanks in it, you'll have to
use double quotation marks in the "net stop" command - e.g.:
net stop "service name"
 
I verified that XNETS is the name of the service, which does work in the
script file.

Thanks for your suggestion.

Mick
 
Try using "sc stop" instead of "net stop".


For processes:

ProcessUtility 2.03
2003 Craig Peacock
http://www.beyondlogic.org


Command Line Process Viewer/Killer/Suspender

Usage:

ProcessUtility.exe [-v] [-t] [-c]
ProcessUtility.exe [-q] [Process Name/PID] [timeout sec(optional)]
ProcessUtility.exe [-k] [-s] [-r] [Process Name/PID]
ProcessUtility.exe [-p] [Process Name/PID]
{RealTime|High|AboveNormal|Normal|BelowNormal|Low}
ProcessUtility.exe [-a] [Process Name/PID] [Mask(To Set)]

-v View Processes.
-t View Kernel and User CPU Times.
-c View Process Creation Times.
-q Send WM_CLOSE Message. Default timeout is 60 Sec
-k Kill Process. (Terminate)
-s Suspend Process.
-r Resume Suspended Process.
-p Set Process Priority.
-a Get/Set Affinity Mask of Process.

PROCUTIL -q [PID] 5


ju.c
 
Thanks ju.c,

Just saw your reply. I will try your suggestions. While I have a script
file that works most of the time, occasionally it fails to stop XNETS.

ju.c said:
Try using "sc stop" instead of "net stop".


For processes:

ProcessUtility 2.03
2003 Craig Peacock
http://www.beyondlogic.org


Command Line Process Viewer/Killer/Suspender

Usage:

ProcessUtility.exe [-v] [-t] [-c]
ProcessUtility.exe [-q] [Process Name/PID] [timeout sec(optional)]
ProcessUtility.exe [-k] [-s] [-r] [Process Name/PID]
ProcessUtility.exe [-p] [Process Name/PID]
{RealTime|High|AboveNormal|Normal|BelowNormal|Low}
ProcessUtility.exe [-a] [Process Name/PID] [Mask(To Set)]

-v View Processes.
-t View Kernel and User CPU Times.
-c View Process Creation Times.
-q Send WM_CLOSE Message. Default timeout is 60 Sec
-k Kill Process. (Terminate)
-s Suspend Process.
-r Resume Suspended Process.
-p Set Process Priority.
-a Get/Set Affinity Mask of Process.

PROCUTIL -q [PID] 5


ju.c


mickey said:
I verified that XNETS is the name of the service, which does work in the
script file.

Thanks for your suggestion.

Mick
 
Back
Top