Heres another way. Not fully tested so use with care
Code:
;############# Below is the auto-executing & persistent code ######################################
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.Avoids checking empty variables to see if they are environment variables.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
#persistent
#SingleInstance force
SetTitleMatchMode, 2
#MaxMem 2
#InstallKeybdHook
#NoTrayIcon
DetectHiddenWindows On
;############# Set timer for Process checker ######################################################
SetTimer, processchecker, 5000
;############# below is the timer for Process checker ############################################
; command line switches for defrag.exe are below
; defrag volume [-a] [-f][-v] [-?]
; http://support.microsoft.com/kb/283080
; start defrag
;run defrag.exe -c
Run, %comspec% /C defrag -c,,UseErrorLevel hide
Return
processchecker:
; not sure if should be checking DfrgNtfs.exe OR defrag.exe or both
Process, Exist, defrag.exe ; check to see if Defrag (defrag.exe) is running.
NewPID = %ErrorLevel%
If errorlevel
Sleep 900000 ; sleep 15 minutes
Else
{
msgbox, Thank you, I will now restart your computer.
; Force a reboot (reboot + force = 2 + 4 = 6):
Shutdown, 6
}
Return
Enjoy