3D PowerShell logo, credit LerroyLee

Schedule Task Orphans Robocopy Process

A while back, I wrote some code to copy some files off of a Windows 2012 R2 Server to a removable USB drive for data archival and off-site storage. I set up the copy process in a Scheduled Task to synchronize the data using Robocopy. However, I realized the scheduled task is not able to stop the Robocopy.exe process. This is very strange behavior as one would think that the spawning process, PowerShell.exe, would end all sub-processes when it’s killed by the ‘End Task’ button. This is not the case. The Robocopy process continues to copy data even if you attempt to end the task.

I need it to stop when I press the “End Task” button. Suppose I want to yank the drive off the server? Suppose, I want to stop it, modify some data, then start it again. I’ll end up with 2 Robocopy processes copying the same data to the same drive, thrashing that poor little guy to death. Imagine a runaway process copying gigabytes of data with no easy way to stop it.

Of course I could kill the process using Task Manager, but this means I have to remember to do that. Also, how is anyone else supposed to come in behind me and manage this process?

It needs to work as expected. Since killing a parent process does not kill the child processes, we can leverage this to create a process monitor function that spawns an intermediary. This process would monitor the parent process and the child process. If the parent disappears, then it will be responsible for disposing of the child process and itself. Likewise, if the child process disappears, it will just end itself as its work is complete. One final responsibility, it should monitor the exit code of the child process to return that to the parent process so it can know what happened.

In short, here’s a play-by-play.
– Create an object to represent the parent.
– Create another object representing the child.
– Store both Process IDs.
– Spawn the process monitor code as a new process.
– We’ll pass the Process IDs of both the parent and the child wait on it synchronously.
– If the child exits, kill the monitor, Pass the Error code back.
– If the parent exits, dispose of the child process, and kill the monitor.

So after placing that function in your script, you can use it like this:

Of course, the above example is just a mishmash of various command-line arguments for Robocopy – feel free to substitute your own as you see fit. It’s always recommended to set some variables first as follows:

Posted in Uncategorized.

Leave a Reply

avatar
10000
  Subscribe  
Notify of