Batch File as Service - TaskKill not killing task - windows

If I run my batch script as normal from cmd it seems to work fine.
What I'm trying to do is setup my script as a service using nssm. That seems to work as expected except for Taskkill - this isn't killing the desired application for some reason.
Anybody have some suggestions?
This is my taskkill code in my bat file:
TASKKILL /f /t /im "application.exe"

Related

bat file kill itself?

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.

How to kill a task in batch?

I am having problems with the taskkill in batch, I am trying to kill a vbscript that is in sleep for an x amount of seconds.
So basically I want to kill this task:
http://i.imgur.com/nNVf1Fh.png
But I somehow seem to get it wrong, I have no clue what part of the task I have to write in the taskkill x
Thank you in advance!
This will kill it without knowing the pid, however it will kill all instances of wscript.exe.
TASKKILL /F /IM wscript.exe
From the task manager top menu, select to show the Process ID for this running process.
Then on command prompt,
taskkill /pid 1234
Where 1234 id the Process ID you want to kill.
VBScripts do have a timeout parameter - see cscript /?.
taskkill /im wscript.exe /im cscript.exe /f
will kill all running vbscripts.
If you want to be particular how do you tell the running scripts apart.

Batch script to kill a process if not responding in a for loop

I am trying to install some windows standalone update files and to do this I need to use wusu.exe. Every now and again wusu.exe will hang. I have created a batch file called prereqs.bat and in this file I have calls to the wusu.exe. I need the code to kill wusu.exe if it hangs, and retry it again.
This is my code as it stands now:
:PreReqs32
taskkill /im prereqs32.bat /f
taskkill /im wusa.exe /f
when it loops back, it kills both the batch file and wusa.exe
start cmd /k c:\windows\temp\prereqs.bat
An outside process so that I can kill wusu.exe if things go awry.
timeout /t 240 /NOBREAK
this timeout is to wait until the install is complete which is sometimes not enough.
taskkill /im "[wusa.exe]" /fi "STATUS eq NOT RESPONDING"
if "%ERRORLEVEL%"=="1" goto PreReqs32
Is there a way for some sort of FOR loop, with logic to exit if the status is not "not responding"?
Also, as a bonus, is there a way to forgo the timeout and just "wait" for prereqs.bat to be complete before moving on, assuming that wusu.exe has not hung?

Executing a command when closing Batch file

I have a batch file playing a sound in the background while it runs, using the method at Batch file executing a sound silently however, when I exit the batch file, the music will continue to play.
Id there any way to run a command while a batch file is closing, so that the sound file will stop playing?
I've tried an example I found called onexit:
onexit taskkill /im wmplayer.exe
But it doesn't work.
Just :
taskkill /f /im wmplayer.exe
Should work.
First idea i had is another batch:
#ECHO OFF
START "" /W <YourBatchhere>
TASKKILL /im "wmplayer.exe"
should do the job :)
The /W will wait at that point, still YourBatchhere has ended.
Regardless of success or error.

Batch script to close all open Command Prompt windows

I have a .cmd file which I call to open multiple instances of Command Prompt via:
launcher.cmd -fs
launcher.cmd -tds
launcher.cmd -fsd
Each command open a new command prompt.
So what I want to do is create a batch file to automatically close all the opened Command Prompt instead of manually doing it.
Be carefull: you might kill more processes than you want:
taskkill /IM cmd.exe
You can add extra filters:
taskkill /IM cmd.exe /FI "WINDOWTITLE eq launcher*"
use
tasklist /FI "imagename eq cmd.exe " /V
to get a glimpse of what cmd.exe processes will be taskkill-ed
You could add the /F parameter to force the process to close but I would only use that if the process doesn't respond to a normal request.
Just a little note why accepted answer from Rene may not work. I was starting my apps from cmd file like
start "" my.exe -my -args
where my.exe was a console app and it was looking like cmd window I wanted to kill, but process name was not cmd.exe (!) and I had to use command like
taskkill /IM my.exe
So in some cases it worth to check the real process name, for example in the windows task manager.
TASKKILL /F /IM cmd.exe /T
good solution

Resources