Windows Task Scheduler, run task if task isn't running?

4

2

How do I ensure a task is running all the time?

I use speedfan to control my fans and on occasion it crashes or it needs to be restarted. If I manually quit speedfan because it isn't working correctly it doesn't reopen automatically.

How can I use the Task Scheduler to ensure it always runs, even if it isn't running?

Currently it is set to run everytime I log on and is set to restart if the task fails but it still isn't reopening.

Thanks

George Othen

Posted 2016-07-06T20:18:48.173

Reputation: 43

Answers

5

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

Pimp Juice IT

Posted 2016-07-06T20:18:48.173

Reputation: 29 425

0

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.

  1. Grab a coffee and download from the link above.
  2. Install with Sysmon.exe -i in DOS or PowerShell.
  3. 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.

  1. Open Event Viewer
  2. Right click on Event Viewer (Local)
  3. Create Custom View...
  4. Click on the XML tab
  5. Check the checkbox Edit query manually
  6. 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 for Sysmon.

    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 for ProcessTerminate.

    (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:

  1. It's important to set the delay to your requirements e.g. 2 seconds in my case.
  2. Then set Repeat task... at the minimum allowed in TS, then the duration slightly longer by 2 seconds. (Test this per app).
  3. 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

Ste

Posted 2016-07-06T20:18:48.173

Reputation: 361