How do I shut off a USB fan automatically when the Mac goes to sleep?

To expand on the accepted answer, on some desktops, hibernatemode 1 is not available, see the manual page for what's available, go to terminal:

$ man pmset

(q to exit the man page, or esc, then q key)

Scroll way down to "SAFE SLEEP ARGUMENTS", it'll list which hibernatemode is available on your system, my old iMac says 0, 3, 25

I set it mine to 25 (write RAM contents to disk, then full power down). Originally devices like USB hubs won't power off when the system is in sleep mode, now it does.

However doing this will make sleeping and waking much slower. I'm still trying to see if I like this, now I close almost all open apps before putting system to sleep to minimize disk activity. Not totally sure if I like this, might be easier to unplug that USB hub.