We currently have a scheduled task on a server, it runs a bat file which copies files from one machine to another. The file looks like:
#echo off
net use t: \\xxxxx\copy password /user:xxx\xyz /persistent:yes
move t:\*.txt C:\testfiles
net use t: /delete
taskkill /f /fi "USERNAME eq xyz" /im conhost.exe
exit
When the task runs I noticed in task manager cmd.exe and conhost.ext are started, I wanted to stop them once the task is done. Killing the conhost.ext manually seems to kill them both. The above bat file runs fine but the conhost.exe is not killed, I wasn't sure if it can kill itself? As running that line in another bat file works. As currently once the task is done those two are still showing up in task manager.
You can try to get the cmd.exe own process so you'll kill only the current instance of cmd.exe . You can use for example getCmdPID.bat:
#echo off
call getCmdPid.bat
taskkill /pid %errorlevel%
pause
conhost.exe is executed automatically for each console application and exits automatically when the application exits, you don't have to kill it explicitly. So just kill cmd.exe.
Related
I start a program from a scilab script via the command line, start myprog.exe.
After the start my scilab script needs to keep going.
Now I want to stop exactly this instance of the process via the command line too, even if several instances of the same program are running.
Is that possible?
I know how to query via batch files whether a process of this program is running and then stop it, but I don't know how to get the exact allocation.
Is there something like a process id?
I use this to check if the process is running:
tasklist /fi "imagename eq ccx.exe" |find ":" > nul
if errorlevel 1 echo Program is running
if not errorlevel 1 echo Program is not running
Use the command tasklist to view all running tasks with their PID
then
Taskkill /PID 26356 /F
or
Taskkill /IM myprog.exe /F
I need to create a windows batch file (*.bat) file that only runs its commands if certain processes (and batch files) are NOT running.
I have looked at a solution that works for processes (*.exe) here:
How to wait for a process to terminate to execute another process in batch file
I want to do something very similar, however, there is one difficulty: Batch files show up as "cmd.exe" in the "TASKLIST" command.
I want to check if a specific bat file is running, for example: "C:\mybatch.bat", and if it is, wait until it is closed.
Checking if a specific bat file mybatch.bat is running could be a tougher task than it could look at first sight.
Looking for a particular window title in tasklist /V as well as testing CommandLine property in wmic process where "name='cmd.exe'" get CommandLine might fail under some imaginable circumstance.
1st. Can you
add title ThisIsDistinguishingString command at beginning of the mybatch.bat and
remove all other title commands from mybatch.bat and
ensure that mybatch.bat does not call another batch script(s) containing a title command?
Then check errorlevel returned from find command as follows:
:testMybatch
tasklist /V /FI "imagename eq cmd.exe" | find "ThisIsDistinguishingString" > nul
if errorlevel 1 (
rem echo mybatch.bat batch not found
) else (
echo mybatch.bat is running %date% %time%
timeout /T 10 /NOBREAK >NUL 2>&1
goto :testMybatch
)
2nd. Otherwise, check if wmic Windows Management Instrumentation command output could help
wmic process where "name='cmd.exe'" get /value
Then you could detect mybatch.bat in its output narrowed to
wmic process where "name='cmd.exe'" get CommandLine, ProcessID
Note that wmic could return some Win32_Process class properties, particularly CommandLine, empty if a particular process was launched under another user account or elevated (run as administrator).
Elevated wmic returns all properties in full.
What you say happens by default.
To test, crate a new .bat file (let's say 1.bat) and put in it
calc
mspaint
Save and run it.
Calculator will start. You will notice that Paitbrush will launch only when you have closed calculator.
I want to create a task in task shaduler on windows which executed a xx.bat program whenever the program isn't running. The question is, how can i check the program is running or not whenever my computer is ready/on. So i just want to check the bat isn't running. When the bat isn't running, the task shaduler will run the bat.
If it was an exe you want to check, Stephan's comment would do it. But running bat files are just shown as cmd.exe in the task list as long as you don't give the process a name. So the point is to start your bat file with a certain "name". You can achieve this starting with this command:
start "somename" xx.bat
Now it can be found with tasklist and findstr easily:
#ECHO OFF
SET running=0
FOR /f "tokens=*" %%A IN ('tasklist^ /v^| findstr /i /c:"somename"') DO SET running=1
IF NOT %running%==1 (
start "somename" c:\SomePath\xx.bat
)
I'm starting a Java background process (Solr) from a batch file with
start /b java -jar ...
The problem is that start /b will shield the process from SIGINT signals, see help start. This means that taskkill /pid ... won't work and taskkill /f /pid ... will kill the process without letting it execute shutdown hooks first.
Is there another way to start the background process on Windows from a .bat file without opening a window and without shielding it from SIGINT? Or is there another way of sending a signal to the java VM so that Solr shuts down gracefully when running from start /b?
I'd like to use a normal batch script rather than VBScript or similar if possible as this is what most our users probably know best.
Not sure how to do it in a .bat file, but with Powershell you can do this:
Start-Process java -ArgumentList "-jar start.jar <args>" -WindowStyle Hidden
Then you can stop the process normally.
Run get-help Start-Process -detailed for more info on options - you can also easily run the process as a different user and other things that the old start won't let you do.
Have you tested that? I ran:
START /B notepad.exe
and then closed it using
TASKKILL /PID 123456
I found a workaround for my specific case (solr + jetty), see http://alaminsumon.blogspot.com.br/2009/06/how-to-stop-and-start-jetty-server-from.html
I am writing a small shutdown script for a java application. I get the pid and then try to do the following:
taskkill /t /pid !pid!
If I am running the command from the console (typing it by hand) everything works fine. I see the shutdown routine being executed in the java console and afterwards the java console is gone. I sometimes see the windows "process not responding. kill now or wait" dialog after a few seconds but the dialog is automatically closed when the java program stops.
If i execute a bat file with the same command I immediately see the "Process not responding..." dialog. I see the console output: "process was successfully shut down" but the java console is still open. It didn't even initiate the shutdown sequence.
edit: the complete batch script
#echo off
setlocal enabledelayedexpansion
if exist .\lock (
set /p pid=< lock
taskkill /t /pid !pid!
)
On the command line !pid! will normally not expanded, as delayed expansion is disabled and can only be enabled by the registry ey or with cmd /V:on.
So at the command line !pid! will stay unchanged and in the batch it will be expanded to the content of the pid variable.
Edit
As you show the correct command on the cmd line, the command in the batch must be wrong.
You should add an echo after the set/p line
Echo pid="!pid!"