Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the PID of a Windows service by the name of the service

Is there a way of getting the PID of a windows service with a command in a batch script by just knowing the name of the service?

like image 468
ilce Avatar asked Jun 19 '15 15:06

ilce


People also ask

How do I find the PID of a Windows service?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column.

How do you find the PID of a process?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.

How do I find service name for a service?

To find the service name and display name of each service on your system, type Get-Service . The service names appear in the Name column, and the display names appear in the DisplayName column.


2 Answers

Try the following code:

FOR /F "tokens=3" %%A IN ('sc queryex %serviceName% ^| findstr PID') DO (SET pid=%%A)
 IF "!pid!" NEQ "0" (
  taskkill /F /PID !pid!
 )
like image 192
Inquisitive Avatar answered Oct 11 '22 10:10

Inquisitive


It's much easier to just do taskkill /f /fi "SERVICES eq <service_short_name>"

like image 37
Matthew Carlson Avatar answered Oct 11 '22 09:10

Matthew Carlson