Gracefully kill defrag.exe when run in command prompt

4

1

When I run defrag in command prompt is there any way to gracefully stop the process before it finishes?

Does killing the task in TM cause any problems if it is in the middle of moving a file?

Windows 7 Pro 64bit

Moab

Posted 2011-08-08T01:17:07.203

Reputation: 54 203

Answers

4

In theory, it should be safe to just terminate the program (in your case you can use CTRL-C) because an effort is made in the design of the Microsoft Disk Defragmenter application to prevent data loss in the event of an unexpected interruption. This is documented on Microsoft's web site as follows:

The following steps are the processes by which Disk Defragmenter and
file system defragment an NTFS volume and update the MFT.

   1. Locate the fragments of each file throughout the disk.
   2. Copy the fragments to contiguous locations on the disk.
   3. Verify that the copy is an exact duplicate of the original file.
   4. Update the MFT in NTFS with the new location of the beginning of
      the file.
   5. Free up the space previously used by the file.

  Microsoft - How Disk Defragmenter Works
  http://technet.microsoft.com/en-us/library/cc778290(WS.10).aspx
  (the above text can be found under "Updating the Master File Table")

Essentially, the data is copied to new sectors, the pointers to the data that was moved are updated to point to these new sectors, and then the old sectors are freed as a result. This is a very safe approach to use in case there is a crash because in a worst-case-scenario (like a crash into a blue screen) you'd have two copies of the same data, one on unallocated sectors, and the other on allocated sectors. From the perspective of reading the data, it will appear the same either way.

Additionally, there are many improvements that can be made to the Disk Defragmentation techniques that Microsoft uses (which are already more than adequate for many people), and a variety of alternative products are examined and compared in great detail here:

  Defragger (from Roedy Green's Java Glossary)
  http://www.mindprod.com/jgloss/defragger.html

Randolf Richardson

Posted 2011-08-08T01:17:07.203

Reputation: 14 002

1Excellent as usual Randolf! – Moab – 2011-08-08T02:39:54.073

7

Control C to cancel it.

I don't expect the Task manager to do any harm, since it first asks the program to close before it brute forces it.

surfasb

Posted 2011-08-08T01:17:07.203

Reputation: 21 453

Reason I asked is a few years ago I thrashed a XP installation, forcing defrag to close using the gui, it would not respond and so I killed it in TM, attempted several tries but TM finally killed it, upon reboot it was thrashed. I was impatient in those years of my life. – Moab – 2011-08-08T01:45:34.533

Yeah, killing by TM isn't always a good idea. One of the most difficult lessons I learned from C++ is that creating a process is easy. Closing a process gracefully is what separates the boys from the men. – surfasb – 2011-08-08T01:48:23.203

Its the brute force part that concerns me, its why I asked if there was a graceful method. – Moab – 2011-08-08T01:53:38.593

1@Moab, just use Ctrl+C; it doesn’t kill the process since you can see it continues afterwards (it detects the keypress, aborts defragmenting, then prints out a message). I recall back in the DOS days, that there was a way to install a Ctrl+C handler (I used one in a Pascal program), so it’s no surprise that Windows-7 console apps can do it. – Synetech – 2011-08-08T02:01:15.367

@moab: don't use TM. Or get a copy of mydefrag which gives you even more granular control over your defrags. Don't worry. It uses the same APIs as defrag. Come to think of it, I can't remember the last time I had to fire up the defrag program. It had to be before Vista. . . – surfasb – 2011-08-08T02:10:30.600

That is not the issue, was looking for an answer to my question, thanks, I know of a dozen defraggers. Looking to learn something, looks like Synetech inc has the best explanation so far, should be added to surfasb answer. – Moab – 2011-08-08T02:37:34.640

Umm, my answer doesn't have Ctrl C? – surfasb – 2011-08-08T04:15:01.197

2

This thread has a lot of helpful information about whether there are dangers to disks that were defragmenting during power failure (while not identical to your situation, you'd still be abruptly halting the program -- as the commenter kindly pointed out, if the system can recover from a hardware interruption, a software interruption probably won't hurt).

It's seems as though defrag is following through with the journaling that occurs during normal use.

jonsca

Posted 2011-08-08T01:17:07.203

Reputation: 3 889

1I think you might want to add something like "if even a hard shut off probably won't damage it, then for a software shutdown won't" – soandos – 2011-08-08T01:47:43.447

Nice article but does not really answer my question on how to gracefully terminate. – Moab – 2011-08-08T01:48:30.800

@soandos That's a good point. Really, both ways are stopping the software before it has a chance to clean up. – jonsca – 2011-08-08T01:50:00.123

@Moab I think both of us are saying that the conventional methods of Ctrl-C or killing it from the task manager aren't going to hurt anything. Just run a chkdsk (or equivalent) to make sure. – jonsca – 2011-08-08T01:51:28.397

1Yes, but this way you more directly answer the question, just something you might want to add to your answer. – soandos – 2011-08-08T01:51:38.107