Windows Task Scheduler, run task if task isn't running?
Solution 1:
Task Scheduler - run task if it isn't running
You can use a batch script similar to the below and use Tasklist and FindStr to check whether or not the EXE name of SpeedFan is running in memory. With the below logic it'll Start the EXE if is not found running in memory.
Batch Script Example
Be sure to replace the SpeedFan.exe
value with the actual name of the EXE file that runs when you launch the app and it's working properly if it's something different in the SET EXEName=SpeedFan.exe
.
You will need to ensure the full explicit path to the EXE is also in the below logic of what it actually is so just replace that (in the SET EXEFullPath=C:\Program Files\SpeedFan\SpeedFan.exe
) with the real path of the app EXE; after the =
sign is where you'll change that.
Just scheduled this to run with Task Scheduler every 1 minute, 30 seconds, or however often you'd like this process to check if it's running or not and if not to then start it.
@ECHO OFF
SET EXEName=SpeedFan.exe
SET EXEFullPath=C:\Program Files\SpeedFan\SpeedFan.exe
TASKLIST | FINDSTR /I "%EXEName%"
IF ERRORLEVEL 1 GOTO :StartSpeedFan
GOTO :EOF
:StartSpeedFan
START "" "%EXEFullPath%"
GOTO :EOF
Solution 2:
Yes, but you first need to install Sysmon to monitor these types of events as not all programs do this. to the least of my knowledge.
This will enable you to restart any app it collects in the ProcessTerminate
event and restart in any time after you want. You will not be limited to the 30 seconds as like using a Batch Script.
- Grab a coffee and download from the link above.
- Install with
Sysmon.exe -i
inDOS
orPowerShell
. - No need to restart.
What this does fully, is found here.
TL;DR
System Monitor (Sysmon) is a Windows system service and device driver that, once installed on a system, remains resident across system reboots to monitor and log system activity to the Windows event log
Now that's installed you can find it located in the Event Viewer
.
On Vista and higher Events are stored in
Applications and Services Logs/Microsoft/Windows/Sysmon/Operational
On older systems Events are written to the
System event log
.
For this example, I'll use stickies and Windows 10 Pro.
N.B. Before using the code in the Task Scheduler it's best to test first in Event Viewer
.
- Open
Event Viewer
- Right click on
Event Viewer (Local)
Create Custom View...
- Click on the
XML tab
- Check the checkbox
Edit query manually
-
This is an example of the stickies program. Just simply modify
C:\Program Files (x86)\Stickies\stickies.exe
to suit your needs. I will explain to the best of my ability below what each section does forSysmon
.Example of Query:
<QueryList> <Query Id="0" Path="Microsoft-Windows-Sysmon/Operational"> <Select Path="Microsoft-Windows-Sysmon/Operational"> *[EventData[Data[@Name='Image'] = 'C:\Program Files (x86)\Stickies\stickies.exe']] and *[System[(EventID=5)]] </Select> </Query> </QueryList>
(1)
[EventData[Data[@Name='Image'] = 'validFullPath.exe']]
is query for the path. What this does is a search for the location of the program files.exe
. This must be a full path as there's no way to use wildcards such as*
.(2)
and
is the and statement to also match...(3)
*[System[(EventID=5)
is the Event ID forProcessTerminate
.(4) If done correctly, you should see the processes of that particular program in the Main GUI when it was last Terminated. If not open the app up and close it, you should see
Number of events:... New event available
at the very top of Event Viewers UI, hit F5 to refresh and you should now see a new Event entry at the top, all going well.- More detailed info can be found in the Events and Event Filtering Enteries section of the Sysmon page.
- To learn more about Event filtering please see Wikipedia on Filtering using XPath 1.0
- If you have made a mistake you will get this warning message
The Event Log query specified is invalid
. - Fret not, as it won't do any harm. Just try again.
Now that you have the query sorted. All you have to do is set-up the Task Scheduler. See the steps below in the attached image.
Task Schedule Diagram Steps 1-8
Task Schedule Steps Diagram 1-8
FINAL NOTE, REGARDING STEP 4 OF THE DIAGRAM:
- It's important to set the delay to your requirements e.g.
2 seconds
in my case. - Then set
Repeat task...
at the minimum allowed in TS, then the duration slightly longer by 2 seconds. (Test this per app). - Don’t use
Stop all running...
checkbox as this will cause a nasty stop/start loop.
It took me a while to figure this out but I'm very glad I did!, I hope you find this useful