1

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?

  • 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. – DavidPostill Oct 26 '17 at 19:32
3

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
1

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

  • 1
    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? – Vlastimil Ovčáčík Oct 27 '17 at 15:09
  • @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. – realtebo Oct 28 '17 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.