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.
Related
I have a windows batch file which i want to execute as a service. I have found app like alwaysRun but i want to use windows in-build app for this purpose. Can anyone please suggest.
My Use Case is : - I have a batch file which will be executing after every 10 secs. So i have created a normal batch file which calls this bat file and sleeps for 10 secs. So i want to make this second bat file as a service. So that it is called once and when the windows reboots.
This file should be called as a service.
#echo off
:begin
CALL dummyfile.bat
timeout /t 1
goto begin
Please suggest.
I would not do that. You could run the first batch file from a scheduled task.
OR
If you want it to run at startup,
As an example, on Windows 8 you could
Create a VBS file that will completely hide your batch file.
hideme.vbs
Set MyScript = CreateObject("WScript.Shell")
MyScript.Run "C:\wherever\script\is\batch.cmd", 0, False
It can be launched as cscript hideme.vbs
Then open Start / Run and type shell:startup and press enter. Paste a shortcut of the VBS file here.
This will let VBS file call the second batch file in hidden mode each time the PC starts up.
EDIT
In order to kill it, you need to create another batchfile which you can run to find the cmd.exe that is running in the background.
In your original batch file, create a title at the very beginning after #echo off
#echo off
title LOOP
:begin
CALL dummyfile.bat
timeout /t 1
goto begin
Now in your new batchfile, let's call it killLOOP.cmd you add this:
taskkill /F /FI "WindowTitle eq LOOP" /T
This will search for a process with Window title LOOP, then kill it. Just run it when you want to close it.
now in your
I executed batch file as a service, but it didn't work.
NSSM package resolved my problem. here are the steps to follow:
1- Download NSSM package
2- Add NSSM Directory path in Environment Variables Path
3- Execute command like:
nssm install Service_Name
3.1 After executing command mentioned in step 3, it will show a popup to provide
program file path. provide batch file path like "C:/Test/test.bat"
3.2 Click on install and now batch file will be executed as a window Service.
have you ever looked at the native Windows program called sc.exe ? It allows you to run a program as a service.
Here's some Microsoft documentation on it: https://support.microsoft.com/en-us/help/251192/how-to-create-a-windows-service-by-using-sc-exe
sc [Servername] Command Servicename [Optionname= Optionvalue...]
In particular, you could use the Create Command to create a new service:
Create
Creates a service (adds it to the registry).
So the simplest syntax would be something like:
sc Create myservice binPath=C:\some\path\to\myservice.bat
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)
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.
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.
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
)