Task Scheduler windows to check the file is running or not - windows

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
)

Related

How to run a .bat file continuously using task scheduler?

I have a simple batch script which is coping file from a folder and then move those files to a different folder. i set a up a task scheduler which working fine. but if another task run the cmd before previous task end, it will mixed up with the copy/move file job. is there anyway we can set the task scheduler that next task will run "only if previous task end"?
here is the screenshot i currently set
https://i.imgur.com/DTcKj9t.png
As mentioned in the comments by Ikegami, You could use a lock file to test if the batch file is running and let the same process delete the lock file when the script is done. You have to be sure though that the script cannot exit unexpectedly.
Another way is to add the following to the top of your batch-file
#echo off
tasklist /FI "WINDOWTITLE eq my_job_run" | findstr /i "cmd.exe"
if not errorlevel 1 exit
title my_job_run
This will check if a process by the name of cmd.exe exists with the window title of my_job_run if it does, exit and if it does not exist, continue and create the title, where the next run will detect it and not run the script again.

Schtaks - Command Prompt Parameters

I have a batch file I need to run ever x minutes. I know how to set up the scheduled task but I'm not sure if there is a parameter I can use to take advantage of the advanced setting "Stop the existing instance".
Basically, I want to be able to run the batch file every x minutes and when it runs again, if for some odd reason the first instance did not complete, I need it to be killed and the new instance to run.
Right now, I'm using:
schtasks /create /tn [TASKNAME] /tr C:\[DIR]\au.bat /sc MINUTE /mo 15 /ru "System"
which is great, but it lacks this extra part.
I already know the GUI is an option but the reason I am using the command prompt is that this is included in an installer clients will be using to make things easy for them and I don't want them to have to take this last step of going into the GUI to make this one change.
EDIT:
Ended up going with something like this:
REM Checks to see if any instances of 'MYTITLE'
FOR /F "tokens=* USEBACKQ" %%F IN (`tasklist /v /fo csv ^| findstr /i MYTITLE`) DO (
FOR /F "tokens=2 delims=," %%B IN (%%F) DO (
taskkill /pid %%B /f
)
)
title=MYTITLE
[BATCH FILE DOES STUFF HERE]
At launch, it looks to see if there are any instances that match MYTITLE and then kills those tasks. Then names itself and runs the batch. If it stays open, it will be killed next time the task it run.
Actually, the quick and easy answer to my question is to simply set up the task in Task Scheduler in the GUI and when all parameters are set, just export the xml file. Once this is done, you just have to create using the /xml option.
schtasks /create /tn [TASKNAME] /XML C:\[DIR]\schedule.xml
You can store the STATUS (i.e. {START, STOP, RUNNING, ...}) of your application along with its PID in environment variables.
For example (environment variables):
MY_APP_STATUS = 1
MY_APP_PID = 1234
For getting PID, see:
getting process ID of exe running in Bat File,
how to get own process pid from the command prompt in windows
In your batch script, you can kill the running batch if the environment variable is set using the PID.
You can use taskkill command to do this:
taskkill /pid 1234 /f
You need to reset the STATUS flag after killing the existing running batch and update the PID with the new running instance.

How to make SVN log display in the window using batch file and Task Scheduler?

I am trying to make a batch file that will check certain SVN repositories for updates each morning. I want to store local repository names in a file (SVN_check_list.txt) and have the console show me the list. My code, posted below, works when I just run the batch file:
#echo off
echo Checking for updates...& echo.
for /F %%A in (SVN_check_list.txt) do (
echo Checking '%%A'
svn status %%A -u )
pause
However, when I try to run it through Windows Task Scheduler (while I am logged in), it runs the code but does not display anything until the 'pause' at the end. When I turn echo on, it shows the commands (svn status -u) but not the output. How can I make this batch file display the outputs of the svn status command even when I run it with task scheduler?
Try passing cmd as the Program/Script to run in Scheduler with arguments /k "C:\My Batch File Folder\MyScript.bat"
This will launch the console.
I found this solution that seems to work: Run a batch file with Windows task scheduler
Basically:
Action: Start a program
Program/script: cmd
Add arguments: /k "C:\Users\tanderson\Documents\setup.bat"
Start in: C:\Users\tanderson\Documents (No quotes)

Wait for a .bat file to close within a windows batch file

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.

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.

Resources