Skip to content

Instantly share code, notes, and snippets.

@huksley
Last active April 10, 2025 04:50
Show Gist options
  • Save huksley/564be2c903312bcee7dffe415d128f90 to your computer and use it in GitHub Desktop.
Save huksley/564be2c903312bcee7dffe415d128f90 to your computer and use it in GitHub Desktop.
Disabling photoanalysisd

For what it's worth (and with all the usual disclaimers about potentially making your mac unstable by disabling system services), here's some commands that will manipulate this service and services like it. Note the $UID in the command, that's just a bash shell variable that will resolve to some number. That's your numeric UID. You just run these commands from a Terminal command line. No special privileges needed.

If you want to disable it entirely, the first command stops it from respawning, and the second kills the one that is currently running:

launchctl disable gui/$UID/com.apple.photoanalysisd
launchctl kill -TERM gui/$UID/com.apple.photoanalysisd

(If you kill it without disabling it will die, but a new one will respawn and pick up where the old one left off)

I don't have this problem myself, so I can't try these next two commands. They're relying on good ole UNIX signals. You could theoretically suspend and resume the process like this ("STOP" and "CONT" are stop and continue):

launchctl kill -STOP gui/$UID/com.apple.photoanalysisd
launchctl kill -CONT gui/$UID/com.apple.photoanalysisd

I don't know what launchd does when running processes are suspended for a long time. Will it detect them as dead and kill and restart them? I dunno. But I do know they won't get any CPU time.

@aselvan
Copy link

aselvan commented Feb 22, 2025

As for the interval that is also up to you based on how aggressive your instance of photo/mediaanalysisd is being about restarting itself, some people here have mentioned it popping back up within a minute, but mine seems more tame, so I picked every 3 minutes and it seems good enough for my case.

Thanks for your response. In my case, the restart intervals are wildly random, ranging from 10 seconds to 1, 2, 3, or even 5 minutes, all in an unpredictable pattern. I decided to run the cron job every minute so that, in the worst-case scenario, these CPU hoggers will only run for up to a minute. It is not a good idea to go lower than a minute with the methods discussed on that link. Even for 1 min, I had to protect the runs with flock to prevent them from stepping on each other due to potential race conditions like so below.
*/1 * * * * /opt/homebrew/bin/flock -no /tmp/kill_pigs.lock ${SCRIPTS_GITHUB}/macos/macos.sh -ckill

Alternatively, I did also find a potential different solution, northpolesec/santa, which might be able to prevent the process entirely(?) but I have not tested it yet ...

Thanks for the link. That sounds interesting. I will check it out; if not for this purpose, it's still a good thing to check out.

@RCurious
Copy link

RCurious commented Feb 22, 2025 via email

@aselvan
Copy link

aselvan commented Feb 22, 2025

Santa is essentially a binary whitelisting and blacklisting system for macOS, which could potentially give you more control over which processes are allowed to run on your machine. If the troublesome processes can be identified by their binaries, Santa could help you prevent them from running altogether, rather than just killing them after they start.

Sounds very interesting, and I will definitely check it out. It is a better approach than a cron job for sure. Given that Apple will likely ignore this and continue to add "features" that no one asked for with no ability to stop or disable them, a solution like this, though not ideal, is the only option we have.

@aselvan
Copy link

aselvan commented Feb 22, 2025

As for the interval that is also up to you based on how aggressive your instance of photo/mediaanalysisd is being about restarting itself, some people here have mentioned it popping back up within a minute, but mine seems more tame, so I picked every 3 minutes and it seems good enough for my case.

One interesting thing I found is, when I disabled the Spotlight (another thing I don't need or care) completely on all volumes, I noticed the frequency of restarts slowed down to 3 tries every minute and nothing for almost 45 minutes (see the log below). So I changed my cron runs to 5 minutes which works well.

$ log stream  |grep --line-buffered 'Activity'|grep --line-buffered "mediaanalysisd: (libsystem_secinit.dylib"
2025-02-22 10:01:05.686981-0600 0x146a3    Activity    0x408c0              10415  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 10:02:11.958896-0600 0x148ce    Activity    0x41030              10470  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 10:03:01.555609-0600 0x14ada    Activity    0x411c0              10571  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 10:47:30.378942-0600 0x1a216    Activity    0x47260              13266  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 10:48:03.646497-0600 0x1a3aa    Activity    0x47a80              13329  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 10:49:10.554892-0600 0x1a5fd    Activity    0x48320              13390  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 11:45:45.342737-0600 0x211d1    Activity    0x4ef80              16666  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 11:46:01.902311-0600 0x212d6    Activity    0x4f690              16722  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 11:47:09.109400-0600 0x21501    Activity    0x4fea0              16778  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 12:00:30.359343-0600 0x22f8d    Activity    0x52200              17571  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 12:45:30.376616-0600 0x28489    Activity    0x57710              20149  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 12:46:04.513946-0600 0x285fa    Activity    0x57ea0              20205  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 12:47:10.967120-0600 0x28863    Activity    0x587c0              20270  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 13:32:16.070683-0600 0x2defd    Activity    0x5e710              22863  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 13:45:45.328875-0600 0x2f85d    Activity    0x60570              23608  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 13:46:02.428745-0600 0x2f958    Activity    0x60c80              23663  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox
2025-02-22 13:47:09.197424-0600 0x2fb90    Activity    0x61470              23722  0    mediaanalysisd: (libsystem_secinit.dylib) AppSandbox

While I was at it, I also added a few other things to my kill list that I don't use or care about, which keep spawning for no reason at all. The total list is below. note: I don't use stock widget or photos or cloud sync etc.

mediaanalysisd mediaanalysisd-access photoanalysisd photolibraryd cloudphotod Stocks StocksKitService StocksWidget StocksDetailIntents
Finally, if anyone is interested, you are welcome to use my script https://github.com/aselvan/scripts/blob/master/macos/macos.sh from my github (available with brew as well). Setup a cronjob as shown below (SCRIPTS_GITHUB is the environment variable pointing to the scripts install directory). There are many other scripts in that repo that you may find useful as well.

SCRIPTS_GITHUB="/Users/arul/src/scripts.github"
*/1 * * * * /opt/homebrew/bin/flock -no /tmp/kill_pigs.lock ${SCRIPTS_GITHUB}/macos/macos.sh -ckill -v >/tmp/kill_pigs.log 2>&1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment