Kill a windows service using its Service Name - windows

I am looking for a way to kill a windows services using its service name rather than the process name, or PID. The two obvious choices are pskill or taskkill, but I cannot seem to find a way of using either of these methods to kill the service by name.
Is it possible to do it by the service name? If so, is anyone able to provide a quick example?

You can use taskkill to filter by service name and kill the service you're looking for.
taskkill /F /FI "SERVICES eq yourservice"

Do you actually want to KILL the process (e.g. if it is frozen) or do you want to STOP the service?
If you want to kill it stick to SomethingDark's answer (taskkill /F /FI "SERVICES eq yourservice").
If you want to stop it use SC STOP "servicename".

In case you have multiple services with the same image name, using the SERVICES filter may not be sufficient. In this case, you need to use more filters like IMAGENAME, PID, STATUS or more.
See my related answer here

with taskkill loop (be careful with closing system processes) (save winservices.bat and run as admin)
call :winservices "service_name1"
call :winservices "service_name2"
:: funcion winservices
#echo off
goto:eof
:winservices
set winservices=%1
taskkill /f /im "%winservices%" /t
goto:eof
source: https://serverfault.com/questions/1005487/how-to-stop-start-and-delete-a-windows-service-with-a-reference-to-the-service

Related

How to kill a specific task in windows 2012 server

This is for windows 2012 server. I am trying to kill a specific task running under cmd window.
In this specific case I want to kill SWIMAUX. I have a command prompt open and every time I use taskkill command I get the following error. How can I kill that task?
C:\Siemens\hot_cold_backups>taskkill /f /fi "imagename eq SWIMAUX"
INFO: No tasks running with the specified criteria.
C:\Siemens\hot_cold_backups>taskkill /f /fi "services eq SWIMAUX"
INFO: No tasks running with the specified criteria.
You need to either specify the image name for the process you want to kill or the window title.
If the window has a specific title, then you can use the WINDOWTITLE parameter of the taskkill command
TASKKILL /F /FI "WINDOWTITLE eq SWIMAUX"
To find the image name of a process:
Open task manager and right click on the process you want to kill, and select Go to details
Find the highlighted executable and use it in your taskkill command, eg. taskkill /f /t /im process.exe
step 1:netstat -ano | findstr :
You should be known your PORT number
Step 2:taskkill /PID /F
id will be shown in the end of the TCP connection data in the result of above command

Closing a window which pops up while running batch file

There is this application build process that I am trying to automate. For this i wrote a java file, which runs every 24 hours.
A batch file is called from here that runs the application build whenever it is called.
I've run into a small problem, when the build fails due to incomplete or invalid files, a window pops up which tells me to look at the logs.
Since I haven't written the build files, I'm not really sure where this gets created from. I wanted to know if I can close this window while the process runs from the bat file.
It may be possible using taskkill, but you'd have to devise a filter that would ideally only match the process displaying the window and never match any other process. Something like:
taskkill /im program.exe
or maybe:
taskkill /fi "windowtitle eq title*"
You might also want to include the /f flag for forceful termination.
You'd also have to try and make sure that the taskkill command doesn't run too quickly and precede the creation of the popup window. You could try to query for the existence of such a process/window; your best bet here is probably wmic. Maybe:
#echo off
setlocal enabledelayedexpansion
set title=Notepad
set pid=
for /f %%i in ('wmic process where "caption like \"%%!title!%%\"" get processid^| findstr /r [0-9]') do #set pid=%%i
if "!pid!" neq "" taskkill /f /pid !pid!
There's no guarantee this will always work, but it's probably the best you can do.

How to stop a service in cmd only knowing the name of the .exe file?

I need to stop a windows service in a batch file without knowing the name of the service. The only thing I know is that the file running is called SomeServer.exe but the SC command requires the actual name of the service.
Currently I have to scan a config file and perform ugly string operations but I hope there is a smarter way.
Any suggestions?
for /f "tokens=2 delims=," %%a in (
'wmic service get name^,pathname^,state /format:csv ^| findstr /i /r /c:"SomeServer\.exe.*Running$"'
) do sc stop "%%a"
It retrieves the system name, service name, path name and state of the services in csv format. The list is filtered for the required executable name in Running state, splitted using the comma as separator, and the second field (the service name) is used to stop the service
this may be helpfull
http://richarddingwall.name/2009/06/18/windows-equivalents-of-ps-and-kill-commands/
If you’ve ever used Unix, you’ll no doubt be well-aquainted with the
commands ps and kill. On Windows, the graphical Task Manager performs
these roles pretty well, but if you ever find yourself needing to
resort to the command line to kill a process (e.g. for some reason on
the Vista machine I am writing this on Task Manager just sits in the
system tray flashing instead of opening), the Windows equivalents of
ps and kill are tasklist and taskkill:
tasklist /v - equivalent to ps aux
taskkill /f /im ncover* - equivalent to kill -9 ncover*
and there is also pslist http://technet.microsoft.com/en-us/sysinternals/bb896682.aspx
edit:
for services use psservice http://technet.microsoft.com/en-us/sysinternals/bb897542.aspx
or use the method described here (using the registry) https://stackoverflow.com/a/298823/1342402

RDP kill a program specifically

I know that you can use tskill to kill processes in a batch file, among other things, but...I have users that remote desktop into a Windows Server 2003 box to run a Microsoft Access program. Occasionally, if someone RDPs in the Access Program will already be open (meaning they have entered someone else's session). This means they are using someone else's Access log on and I have certain forms that record that info and use it to autoemail people reminders. Since everyone uses the same program only on the server, I have all rdp login as the same user. When I tried to do a tskill batch program for msaccess.exe, it killed the Access of everyone logged in--doh! I can't see making everyone log in when they rdp in, so I am hoping the answer isn't make a log in for everyone in the enterprise and then get to their computers to change their save rdp log on information. Is there a way to run something like tskill for ONLY the current session? The batch command I was using is taskkill /f /im msaccess.exe . Thank you in advance for your time and your replies.
I believe this script will do the trick.
:: Find the current session.
FOR /F "tokens=3 delims= " %%F IN ('query session %USERNAME%') DO SET SESSIONID=%%F
:: Kill all msaccess processes *in this specific session*.
taskkill /FI "SESSION eq %SESSIONID%" /IM msaccess.exe /F

Any way to write a Windows .bat file to kill processes? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Every time I turn on my company-owned development machine, I have to kill 10+ processes using the Task Manager or any other process management app just to get decent performance out of my IDE. Yes, these are processes from programs that my company installs on my machine for security and compliance. What I'd like to do is have a .bat file or script of some kind with which I can kill the processes in question.
Does anybody know how to do this?
You can do this with 'taskkill'.
With the /IM parameter, you can specify image names.
Example:
taskkill /im somecorporateprocess.exe
You can also do this to 'force' kill:
Example:
taskkill /f /im somecorporateprocess.exe
Just add one line per process you want to kill, save it as a .bat file, and add in your startup directory. Problem solved!
If this is a legacy system, PsKill will do the same.
taskkill /f /im "devenv.exe"
this will forcibly kill the pid with the exe name "devenv.exe"
equivalent to -9 on the nix'y kill command
As TASKKILL might be unavailable on some Home/basic editions of windows here some alternatives:
TSKILL processName
or
TSKILL PID
Have on mind that processName should not have the .exe suffix and is limited to 18 characters.
Another option is WMIC :
wmic Path win32_process Where "Caption Like 'MyProcess%.exe'" Call Terminate
wmic offer even more flexibility than taskkill with its SQL-like matchers .With wmic Path win32_process get you can see the available fileds you can filter (and % can be used as a wildcard).
I'm assuming as a developer, you have some degree of administrative control over your machine. If so, from the command line, run msconfig.exe. You can remove many processes from even starting, thereby eliminating the need to kill them with the above mentioned solutions.
Get Autoruns from Mark Russinovich, the Sysinternals guy that discovered the Sony Rootkit... Best software I've ever used for cleaning up things that get started automatically.
Download PSKill. Write a batch file that calls it for each process you want dead, passing in the name of the process for each.
Use Powershell! Built in cmdlets for managing processes. Examples here (hard way), here(built in) and here (more).
Please find the below logic where it works on the condition.
If we simply call taskkill /im applicationname.exe, it will kill only if this process is running. If this process is not running, it will throw an error.
So as to check before takskill is called, a check can be done to make sure execute taskkill will be executed only if the process is running, so that it won't throw error.
tasklist /fi "imagename eq applicationname.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "applicationname.exe"
Here I wrote an example command that you can paste in your cmd command line prompt and is written for chrome.exe.
FOR /F "tokens=2 delims= " %P IN ('tasklist /FO Table /M "chrome*" /NH') DO (TASKKILL /PID %P)
The for just takes all the PIDs listed on the below tasklist command and executes TASKKILL /PID on every PID
tasklist /FO Table /M "chrome*" /NH
If you use the for in a batch file just use %%P instead of %P

Resources