Use wmic to capture the percentage of cpu usage for a specific service, but wish the same to determine my memory usage, is it possible to add the same line ?:
wmic path Win32_PerfFormattedData_PerfProc_Process get
Name,PercentProcessorTime | findstr /i /c:w3wp
Or it needs to be in a different line?
command with which I can make that query?
Tks for your help!!
This works in Windows CMD prompt,
tasklist /fi "services eq ServiceName"
Put your service name in 'ServiceName'
I am not sure if this is what you are asking.
Related
I am trying to run a batch file that includes some WMIC queries on multiple versions of Windows. Windows 2003 causes the script to hang. It is most likely due to the first time that wmic is being run. The computer will normally output "Please wait while WMIC is being installed.."
Is there anyway to check if wmic is installed and if it is not, do not run it? I do not want to install WMIC on the computers I am running this on if it is not already installed. Should I just skip this query on all Windows 2003?
May be I'm wrong, but I think wmic is present at least from XP
This may help
#echo off
where /R c:\windows\ wmic.exe >nul 2>nul || (echo/ wmic.exe not found & exit/B)
rem wmic queries here
exit/B
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
OS: Windows XP, Windows 7 64bit.
We have some fairly hefty cmd scripts that are used for some daily build processes. These scripts spawn numerous other (windowed) processes. There is one controlling cmd script, a small simple script, which starts the main cmd script. The purpose of the small controlling script is to clean up in situations where the main script or any of its children fail. This is accomplished fairly easily: the main script and all its children have window titles which begin with a unique identifier. When the controlling script determines that the main script and all its children should have completed, it uses tasklist to find windows of any hung processes, via:
tasklist.exe /FI "WINDOWTITLE eq UniqueIdentifier*"
This all worked very nicely in XP. Now enter Windows7 64-bit. Here, if the main .cmd script or any other .cmd shell window attempts to sets its window title via
title UniqueIdentifier Followed By Descriptive Text
Windows7 64-bit kindly prepends other text to the title (specifically, "Administrator: " or similar). The prepended text cannot be relied upon. So now we want to use
tasklist.exe /FI "WINDOWTITLE eq *UniqueIdentifier*"
but THIS FAILS with the error message "The search filter cannot be recognized". Going the route of using our UniqueIdentifier as a post-fix does not work: the command
tasklist.exe /FI "WINDOWTITLE eq *UniqueIdentifier"
also results in the same error message. It seems that Microsoft's notion of "wildcard" in a filter does not extend beyond having a "*" as the terminal character. Ouch.
DOES ANYONE HAVE ANY WORK-AROUNDS? Pslist does not seem to allow filtering with window title.
You can use the /V option to include the window title in the output and then pipe the result to FIND (or FINDSTR) to filter the result.
tasklist /v | find "UniqueIdentifier"
tasklist /v | findstr /c:"UniqueIdentifier"
If using FINDSTR then I recommend using the /C option so that you can include spaces in the search string.
You might want to use the /I option if you need to do a case insensitive search.
Yes, it works fine if the * is at the end of the process name
searched for,
as pointed out in
the answer below.
Here is an example of how to run the command :
tasklist /FI "IMAGENAME eq no*"
Based on my experimentation, the wildcard for taskkill only seems to work at the end of a string, not in any other position. I can't find any documentation from Microsoft one way or another about this. However all of the examples in the documentation follow this format
Successful: notepad*
Fails: notepad*.exe
Fails *notepad*
As mentioned in the other answer, best to parse the output of tasklist to get exactly what you want rather than relying on the rather broken behavior of taskkill.
I think this works on Windows 10. Here's my snippet
set PROCNAME="Foobar"
tasklist /FI "IMAGENAME eq %PROCNAME%*" 2>NUL | find /I /N %PROCNAME%>NUL
if "%ERRORLEVEL%"=="0" (
echo it is running
)
Notice the asterisk in the filter.
Use powershell. Get-Process. way more useful if you can handle the power that is. Try
get-process | where MainWindowTitle -like "*UniqueIdentifier*" | select *
Background: I use DOS START command to start MyDaemon:
#echo off
START "MyDaemon" java -cp test.jar MyTest /B
As part of this, I also want to check if MyDaemon is already running. If it is, I don't want to start it again.
The dos command that doesn't suit my requirement is:
tasklist /fi "imagename eq "MyDaemon" > nul
if errorlevel 1 start "MyDaemon" java -cp test.jar MyTest /B
and that's because, in the tasklist, the image name is "java.exe", not "MyDaemon". I am looking for the "application name" as seen in task manager, not the image name.
So how can I perform this check to see if MyDaemon is already running using DOS?
You may try:
tasklist /fi "windowtitle eq MyDaemon"
Type tasklist /? for further details.
No, you only can enumerate processes. If a process has been launched with parameters (like your'), you cannot see them.
I can give you a solution in .NET to get the application name for processes (or the title of the main window), but I don't think dos can do it.
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