Windows Server Summit 2024
Mar 26 2024 08:00 AM - Mar 28 2024 03:30 PM (PDT)
Microsoft Tech Community

Windows Server ScheduledDefrag Task Schedule not running

Copper Contributor

All my Windows server (2008 R2 and above) unable to run scheduled task of disk defragmentation? I had turn on the schedule defragmentation in disk defragmenter and set it run on weekly basic, but, it not run. 

 

In the schedule defragmentation had selected all drive to defragment. Below is one of the server schedule defragmentation sample:

Scheduledtask.JPG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I need help to solve this problem else i need to manual do all my Windows server disk defragmentation by monthly. 

11 Replies

I'd check the logs at;

Event Viewer\Applications\Microsoft\Windows\TaskScheduler\Operational

(note: log may need to be Enabled in right-hand pane)

 

 

 

Hi Dave,

 

I checked the event viewer log and history in task scheduler, defrag schedule task had run. However, when we see the "defragment and optimize drive" application, it not defrag and optimize the drive. The drive still in high fragmented rate.

 

The default action of defrag schedule is correct?

 It using below arguments for the defrag:

%windir%\system32\defrag.exe -c -h -k -g -$

Yes, that looks correct. Might also check the task Conditions tab and remove the checks.

 

 

I had removed the all the check inside the condition tab but still not defrag the drive when I run the task schedule.

 

I noticed if I change the action argument to defrag.exe -c only, then defrag will run.

Can I know what is the meaning of -h -k -g -$? For your information, I using VM server.

You can find most here.

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/defrag

 

The $ may have to do with priority. If it runs Ok using defrag.exe -c then I'd probably go with that as acceptable.

 

 

@Nivlee I came across the same problem on our two Windows 2012 servers. Defrag had never been run and I suspect the reason is that there is no trigger on the ScheduledDefrag task which means it'll never run automatically. Changing the schedule in the optimisation tool from weekly to daily doesn't make any difference. I suspect this is a bug that's been around a long time...

 

nvPQwU8[1].png

I'm about to add a trigger to run it once a week and see what happens.

I read elsewhere that the -$ flag indicates to defrag.exe that it's running as a scheduled task. It must make it do something different to when run from command line.

In general, some other scheduled task (don't know which) runs ScheduledDefrag. That is why its triggers are empty. I suppose, with some care, triggers could be added. It is too bad that Microsoft allows but doesn't show such cascaded schedules.

I'm seeing all the same symptoms on my server. I just optimized my SSD for the first time since I installed it. Before that, it was showing "Never run".

I too have the blank list of drives under the Choose button.

One extra thing I noticed is that the scheduled task does get triggered and it does log a service start and stop in the System log, but then it seems like nothing else happens. So maybe I need to adjust the scheduled Action as mentioned above.

Your post mentioned Server 2008, but 2008 does not have the arguments of -k -g -$ (in fact, 2008 uses / for arguments, not -), therefore your defrag would immediately exit out with an error status. That likely explains your issue.
Hold your WINDOWS key and tap R and type in CMD and tap enter. There type in DEFRAG.EXE /? and hit enter. Server 2008 has very limited options, nothing for file optimization (which moves the most important files to fastest portions of your disk).
Moreover, you probably should use the Degrag setting that comes in Task Scheduler already. Hold WINDOWS and tap R and type in TASKSCHD.MSC , in the popup's left pane navigate to TASK SCHEDULER LIBRARY > MICROSOFT > WINDOWS > DEFRAG

 

If you want to optimize your files as well, I suggest Auslogics Disk Defrag, it works on all Windows versions & is very fast when compared to Windows' built-in defrag.exe .  If you can get an old version of Ausologics Disk Defrag, it will lack the annoying options to "buy now" and it also lacks the install of Auslogic's other products as trial versions.  Unfortunately, it cannot be scheduled without purchasing, and the old version lacks optimizations for SSD (solid state) drives.


I hope this helps?
Have a great day!
JohnNeu

Hi, I saw this unresolved thread, and just want to add that:

  • -k selects the 'slab consolidation' task, which seems to be specific to virtual disks and Storage Spaces arrays. For example, defrag on hyper-v VMs show disks as 'Thin Provisioned Drive' and talks about 'space efficiency' and 'evictable slabs'. And it actually runs there. I don't know what it actually does... maybe it tells the backend to defragment the virtual disk?
  • -d selects 'traditional defrag' task. Also runs if no task is specified (previous Windows used this).
  • Adding -o overrides the task choice and performs 'proper optimization for each media type'.
  • If you look at Windows 10 ScheduledDefrag task, it has -o. Server2012R2 and above have -k.

This is why servers no longer defragment automatically. I do not know what the official guidance is for server operators. My guess is that you would alter the task or create a new custom one that fits your environment. That would also involve choosing which disks to optimize and which to skip, and choosing what time to run at to avoid conflicting with backups and to not optimize all VMs simultaneously. This complexity may be why it's effectively turned off by default.

 

Regarding the missing triggers and schedule:

  • Classic scheduled tasks, and tasks created in the Task Scheduler UI, use <CalendarTrigger> to define the schedule. This is then visible in the UI.
  • Maintenance tasks instead use <MaintenanceSettings> and integrate into the Automatic Maintenance functionality. You can see this if you export the builtin task and view the xml file. I think this is a Windows 8 feature, and the classic Task Scheduler UI has not been kept up to date.