Reputation: 617
I have a batch file, which will search for a java process and kill the same. The script works fine when the bat file is run on the command prompt. But when I tried to execute in task scheduler, it is not working.
I have selected the option "Run whether the user is logged in or not"
My batch file is as given below:
for /F "tokens=1*" %%i in ('jps -lv^|C:\Windows\System32\find.exe "TaskTest"') do (C:\Windows\System32\taskkill.exe /F /PID %%i )
I have also tried whatever is mentioned in this link Batch runs manually but not in scheduled task
Any other suggestions please.
Note: I have another batch file to archive files which runs properly in task scheduler with the same user account
Upvotes: 5
Views: 33440
Reputation: 1
I believe user1407688 already answered this clearly, but since I too struggled with finding a solution to this problem I'm pasting images if it'll help make it clearer to others who are searching for a solution. In my case the "power" check boxes under "conditions" needed to be unchecked. Click on the links below to see images:
Upvotes: 0
Reputation: 617
Finally I was able to figure out the problem. It worked when I unchecked the option "Start only when the computer is on AC power' under 'Conditions' tab in task properties, combined with the other suggestion of using the 'Program' as
C:\Windows\System32\cmd.exe
and 'Add Arguments' as
/c C:\mypath\myFile.bat
and 'Start in' as
C:\mypath\
This worked when the option 'Run whether user is logged in or not' is selected with 'Run with highest privileges' checked.
Thanks for all those who have spend time to analyse this :)
Upvotes: 22
Reputation: 86
Why are you searching. We do then test, not test then do.
taskkill /f /im TaskTest&&Echo Task Killed||Echo Task not found
To see what your problem is
taskkill /f /im TaskTest > "%temp%\taskkill.log" 2>&1 &&Echo Task Killed >> "%temp%\taskkill.log" 2>&1|| Echo Task not found >> "%temp%\taskkill.log" 2>&1
and look in taskkill.log.
You may also want to do this in the batch as a diagnostic aid. Echoing out your command line m,ay be useful.
tasklist > "%temp%\taskkill.log" 2>&1
set > "%temp%\taskkill.log" 2>&1
Upvotes: 0