How to detect if a specific scheduled task is running?

6,155

Solution 1

Using plain MSDOS, you can do:

schtasks /query /TN my-task-name /FO LIST

And extract the output. As an example I do:

for /F "usebackq tokens=2,*" %%f in (`schtasks /query /TN my-task-name /FO LIST ^| findstr Status`) do set status=%%f %%g
if "%status%" EQU "Running" goto :eof

Solution 2

You can use the Get-ScheduledTask and Get-ScheduledTaskInfo command in PowerShell.

Share:
6,155

Related videos on Youtube

realtebo
Author by

realtebo

Updated on September 18, 2022

Comments

  • realtebo
    realtebo over 1 year

    I've 2 specific scheduled tasks on my windows 2008 Server

    • The first runs once a day
    • The second runs every five minutes.

    In the second task, that is basically a php script, I'd like to check if the first task is running and, if yes, to exit / avoid to run.

    So the question is: is there a way to check from command line if a specific task is running?

    • DavidPostill
      DavidPostill over 6 years
      tasklist - TaskList displays all running applications and services with their Process ID (PID) This can be run on either a local or a remote computer.
  • Vlastimil Ovčáčík
    Vlastimil Ovčáčík over 6 years
    Hi Andre and welcome. Consider improving your answer with actual code or example that illustrates the solution. For example (Get-ScheduledTask -TaskName MyTask).State -eq "Running" returns true if MyTask is running. Did you have other solution, possibly involving the mentioned Get-ScheduledTaskInfo?
  • realtebo
    realtebo over 6 years
    @VlastimilOvčáčík: thanks a lot for your comment! I tried using Where State -eq "Running" but it returns true even if a process is not running, because the get itself run without problem. I'll try your syntax ASAP.