1

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.

Azeem
  • 11,148
  • 4
  • 27
  • 40

2 Answers2

1

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

0

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.

Azeem
  • 11,148
  • 4
  • 27
  • 40
  • The reason why this doesn't work for me is that I'm not using the batch file to run any applications. I'm using it to check current status of windows services and then start them if they are off. The batch file would be running from cmd.exe so this method would not help. – theharlotfelon Aug 09 '17 at 21:27
  • It's not your batch? Can you not add this logic into it? – Azeem Aug 10 '17 at 05:12
  • I used your answer to come up with a solution to my problem. While ultimately I didn't get your solution to work in my case, I came up with something bases on what was stated here from going down the rabbit hole. See above for my answer. – theharlotfelon Aug 13 '17 at 04:23
  • @theharlotfelon: I'm glad that it was of some use in your scenario. :) Would you care to shed a bit light on what you have actually done to solve your problem using bits of this solution? – Azeem Aug 13 '17 at 04:26
  • 1
    I edited my initial question to show what I did. I did run into another issue in testing though... The issue here is that while this would work running the batch normally, when used with Task Scheduler, it just sees that the task is running and instead of opening a new instance and closing the last, it doesn't open as the default for tasks is to do nothing. I know that in these cases, I could easily just change the option in the GUI to close the previous instance, but I'd rather automate the process and may need to look into other options at this point. – theharlotfelon Aug 13 '17 at 15:30