3

This question already has an answer here:

I’m doing some long-running, CPU-intensive work. This means that I am tethered to a power outlet at all times.

What I’d like to do is ask macOS to suspend a particular program when I’m on battery power and resume it when I plug in.

Googling has failed me. Is there any way? I’m a programmer so I don’t mind going low level if necessary.

marked as duplicate by Nimesh Neema, Allan, IconDaemon, nohillside Jan 25 at 7:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • For future readers, the linked answer gave some hints about how to detect battery/power state, but not about how to suspend a program or to detect a power state change without polling. I could not find a way to do either of those. Lucky for me, the program is one that I have written. My solution is to execute pmset -g batt every so often and parse the output (ick). If/when I detect that I'm on battery, I sleep in a loop, executing pmset occasionally to see whether we're back on AC adapter power. Ugly, but sufficient. – Josh Bleecher Snyder Jan 25 at 19:03
  • ...and you can use SIGSTOP and SIGCONT to pause and resume a process. – Josh Bleecher Snyder Jan 26 at 23:17
  • I made github.com/josharian/power/nobatt as a simple utility to solve this. It works only for command line programs, but maybe it’ll be useful for other people in a similar situation. – Josh Bleecher Snyder Jan 27 at 21:00

Browse other questions tagged .