Page 1 of 1

Dos batch file to check scheduled task status

Posted: 12 Dec 2011 15:18
by bbcac
I want to create a batche file that will kick off a scheduled task on a remote server, check the status, then execute some more stuff

I can execute the scheduled task like this
schtasks /RUN /S myserver /TN "my_task_name"

I can check the status like this
schtasks /query /s myserver /fo list /tn "my_task_name" | find /c "Running"

When the query commands returns 0, I need it to execute some more commands afterwards.
Is there a way to put that query command in a loop so that it won't leave until it returns 0?

Re: Dos batch file to check scheduled task status

Posted: 12 Dec 2011 15:36
by orange_batch

Code: Select all

:query

schtasks /query /s myserver /fo list /tn "my_task_name" | find /c "Running"&&(
code to run if successful
)||(
code to run if failed
)

ping -n 2 0 >nul
goto query

Where ping -n 2 is the number of seconds to delay + 1. That is, 2 is a 1-second delay, 3 is a 2-second delay...

Re: Dos batch file to check scheduled task status

Posted: 12 Dec 2011 16:03
by bbcac
Thanks for your help... what is wrong with this code? If I replace goto :decrypt with echo "done" it works as fine (but never stops). But if I put goto decrypt, it goes there right away

REM ** RUn the SFTP collection. THis runs under the context of ent\FTP_EPay
schtasks /RUN /S MYSERV /TN "My_Task"

REM ***********************************************************
REM ** Check the status of the running task
REM ***********************************************************

:query

schtasks /query /s myserv /fo list /tn "My_Task" | find /c "Running"&&(
REM ** code to run if successful
echo "STILL GOING"

)||(
REM ** code to run if failed
GOTO decrypt
)

ping -n 2 0 >nul
goto query


:decrypt
REM ***********************************************************
REM ** Decrypt the files
REM ***********************************************************
for /f %%a IN ('dir /b T:\*.PDF') do call T:\Decrypt.bat %%a %%a

Re: Dos batch file to check scheduled task status

Posted: 12 Dec 2011 16:49
by orange_batch
I'm not sure what you mean. If the task isn't running, it'll goto decrypt. If you want it to keep looping after the decrypt loop, put another goto query after the decrypt code.

Re: Dos batch file to check scheduled task status

Posted: 12 Dec 2011 21:43
by bbcac
What I am saying is that it doesn't act as you say it should. If I leave it like it is, then run the script it goes straight to "Done" and ends execution.

However If I repalce the
Goto :decrypt
line with
echo "DONE"

it will loop through "Still Going" every two seconds, then after 10 seconds it goes to "Done" every two seconds. This follows the logic I indended except the the continuous "Done" message. Rather then it just saying done, I need it to skip to the decryp section.

Re: Dos batch file to check scheduled task status

Posted: 12 Dec 2011 22:14
by orange_batch
Oh, what you mean is you only want it to run decrypt once, after your task is found running, and then NOT running, but then to continue it's regular check.

Code: Select all

:query

schtasks /query /s myserver /fo list /tn "my_task_name" | find /c "Running"&&(
(code to run if successful)
set found=1
)||(
(code to run if failed)
if defined found set found=&goto decrypt
)

ping -n 2 0 >nul
goto query

:decrypt
(decrypt code)
goto query

Re: Dos batch file to check scheduled task status

Posted: 13 Dec 2011 12:07
by bbcac
I need just a little more help. Everything is working fine however some of our users that have windows XP and some have windows 7

I used the /TN parameter on the shdtasks program which works great on Windows 7. On windows XP this parameter is not available.

I tried SCHDTASKS /Query /s Myserver /fo table /nh | find /c "Running" | find /c "FTP_" .This obviously doesn't work since find /c returns a number and not the whole string.

Is there a dos command that I can use to return a 0 or 1 if a there is a line in the output that has both "Running" and "FTP_" in it?

Re: Dos batch file to check scheduled task status

Posted: 13 Dec 2011 12:59
by orange_batch

Code: Select all

SCHDTASKS /Query /s Myserver /fo table /nh | find "Running" | find /c "FTP_"

find /c "Running" :wink:

That is, remove the part in red.