7

Every time I add a few MBss of files to my SD disk, Spotlight starts eating battery (per Activity Monitor Energy Impact) for one to several days indexing them.

How can I make indexing to turn off automatically when on battery power, and enable again when Macbook is powered from an AC outlet?

3 Answers 3

9

jherran's answer helps you with disabling Spotlight indexing, but does not address the part of the question about toggling automatically when AC power is connected or disconnected.

You could use sudo mdutil -i off / and sudo mdutil -i on / in conjunction with a listener for when AC power is disconnected. Using something like ControlPlane will satisfy your requirement for automatic toggling. It supports "Current power source" as an event to listen for. It is free and open source.

You might be against using a third party solution. To roll your own, try writing a script that loops checking pmset -g ps | grep -c 'AC Power' with a sleep interval in between. If AC is connected, it outputs 1 (0 otherwise).

2
  • I would be grateful for a sample scrip doing just that for no-third-party case. And I failed to make ControlPlane work correctly for this purpose.
    – yurkennis
    Mar 16, 2015 at 15:29
  • ControlPlane link is now dead
    – blissweb
    Mar 11, 2022 at 0:36
2

I wrote this script (/Users//bin/control_spotlight_indexing.sh) to toggle on/off indexing based on power source. I also set it up to run as root in a cron job (executed as /Users//bin/control_spotlight_indexing.sh >> /Users//bin/control_spotlight_indexing.log).

Suggestions for improvements are welcome.

#!/bin/zsh

# Turn on Spotlight indexing by default
mdutil -i on /

# Power: 0 is battery; 1 is charger.
power=$(pmset -g ps | grep -c 'AC Power')

while [ true ] ; do

  if [ $power -eq 0 ] ; then
    # Turn off Spotlight indexing
    mdutil -i off /
    # Check every hour
    while [ $power -eq 0 ] ; do 
      time=$(/bin/date)
      echo "[$time] Battery: Spotlight indexing is OFF. Will check again 1 hour."
      sleep 3600
      power=$(pmset -g ps | grep -c 'AC Power')
    done
  fi

  if [ $power -eq 1 ] ; then
    # Turn on Spotlight indexing while on charger
    mdutil -i on /
    # Check every 10 minutes
    while [ $power -eq 1 ] ; do 
      time=$(/bin/date)
      echo "[$time] AC Power: Spotlight indexing ON. Will check again in 10 minutes."
      sleep 600
      power=$(pmset -g ps | grep -c 'AC Power')
    done
  fi

done
1
  • You may wanna add the cron entry that you're using ? Looks like at startup and then sleeping. Maybe more efficient to set the job as something that runs every few minutes in cron and checks for both conditions.
    – blissweb
    Mar 11, 2022 at 0:38
1

Open your Terminal and type the following to disable.

sudo mdutil -i off /

To enable again, replace off with on.

For more info, you could type:

man mdutil
2
  • 1
    As @aglasser suggests, your answer "does not address the part of the question about toggling automatically when AC power is connected or disconnected". Hence the downvote.
    – yurkennis
    Oct 31, 2014 at 21:38
  • Upvote for the first with the basic info. I think the simplest thing is to disable the damned thing the majority of the time. I'm on battery 95% of the time. I use spotlight once every few weeks tops I can live without it. :-)
    – blissweb
    Mar 11, 2022 at 0:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .