1

I have a scheduled task running at 00:00 that opens a .bat file containing a robocopy command, with the task set to stop after 4 minutes, and to force stop if not ending when requested.

It seems the Task Scheduler stop command is ignored until the robocopy command finishes, which seems to me to defeat the point - is there a way around this?

  • The message I would expect is in the history at 00:04, however the output file of robocopy continues running for a further 4 minutes, only stopping at 00:08:

    Task Scheduler terminated {uuid} instance of the Taskname task due to exceeding the time allocated for execution, as configured in the task definition.
    User Action: Increase the configured task timeout or investigate external reasons for the delay.

  • There are other commands in the .bat file, but the robocopy step is the one running at the time the scheduler tried to stop the task, with the command after it not running:
     MkDir "C:\Destination\Folder"
       RoboCopy "C:\Source\Folder" "C:\Destination\Folder" /MOV /IS /S /R:0 /LOG:"C:\output.txt"
       RmDir "C:\Destination\Folder" /s /q
    
2
  • 2
    I believe this is normal behavior, as the task and script are two separate entities - while the task starts the script, the script isn't the task, so it should run to completion independent of the task itself (at least this has been my experience with starting a RoboCopy script in Task Scheduler). Microsoft Docs for Task Scheduler would need to be referenced (IIRC, the task itself garnishes script completion or errors from an event within Event Viewer, although I could be misremembering)
    – JW0914
    Sep 21, 2020 at 10:38
  • 1
    @JW0914 thank you for the edits Sep 21, 2020 at 12:18

2 Answers 2

1

@JW0914 says right:

I believe this is normal behavior, as the task and script are two separate entities - while the task starts the script, the script isn't the task, so it should run to completion independent of the task itself (at least this has been my experience with starting a RoboCopy script in Task Scheduler). Microsoft Docs for Task Scheduler would need to be referenced (IIRC, the task itself garnishes script completion or errors from an event within Event Viewer, although I could be misremembering)

Here we can work around this behaviour, since this is a plain robocopy command with no batch syntax at all, by using Create basic task without creating the batch file:

  1. Open Task scheduler → Create → Create basic task
  2. Name task and set description
  3. Set trigger
  4. Choose time
  5. Action → Run a program:
    • Program/Script: C:\Windows\System32\robocopy.exe
    • Arguments:
      "C:\Source\Folder" "C:\Destination\Folder" /MOV /IS /S /R:0 /log:"C:\output.txt"
      
  6. Finish
3
  • Ah, so the scheduler cannot cancel the copy, only the .bat. Unfortunately there is other commands in the file, but only because whoever wrote it wanted to delete the contents of source folder. I will append the full file to the question Sep 21, 2020 at 12:13
  • @RichardHarris how about creating multiple tasks??
    – Wasif
    Sep 21, 2020 at 13:08
  • This answers my question, though my problem was that the task was occasionally overrunning into another one and it didn't matter if it was done. I've put the .bat files into one task now and they seem to run in sequence. Sep 21, 2020 at 16:01
0

need some help to write an simple script.

need to trigger an proccess when cpu get below 12% and running for 5 min kill the process. any ideas

$processor = (Get-Process).CPU

$processes_to_kill = get-process Omnis* |

Where-Object {$_.$process.cpu -le 12 -and {($_.process.starttime).TotalSeconds -gt 300} }

if ($processes_to_kill -ne $null)

{

$processes_to_kill | ForEach-Object { $_.Kill() }

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .