Stop DVR server when battery is low

I would like to stop the DVR server when my MacBook Pro battery is at a specific value (25% for example) and restart the server when AC power returns. This will allow the MacBook to go into deep sleep and recover when power returns. (I need to stay logged in on the MAC because I need transcoding; I can’t auto login because I need FileVault.)

Any ideas for a configuration change or a Terminal script/process to do this? Getting rid of the Channels “do not sleep” assertion would also work since I have Amphetamine to prevent sleep until a determined battery level.

Maybe applescript?
But honestly you'd be better off with an Intel NUC rather than running on a laptop or really any mini pc with like an n100 chip. Using a laptop as a server is not really a great idea for reasons like this, plus you can also plug a small PC into a small UPS, and that'll handle shutdown.

Given that I have the laptop, all I really need is a command to shut down the channels server when the laptop changes to battery , and resume the server when AC power returns. I can figure out the method of monitoring, but I need to know how to safety stop and start the Channels server.

Okay, so I would ask Claude to write you a simple osascript command. Automation with AppleScript is probably the easiest solution, I would say, but it just has to poll the battery life, which should be able to do. I don't think this is a common use case because most people aren't running a DVR server that's meant to be persistently on on a laptop that's going to shut down. So, I would just ask your friendly LLM to spit out a simple script. I would shoot for AppleScripts, and if that doesn't work, try bash.

That's my best advice. I would say attempt that for your particular use case scenario. I think even a smaller model should be able to handle that request with ease.

-- Save as Application with "Stay open after run handler" checked
property threshold : 25
property agentLabel : "com.getchannels.dvr"

on idle
	set battInfo to do shell script "pmset -g batt"
	if battInfo contains "Battery Power" then
		set pct to (do shell script "pmset -g batt | grep -Eo '[0-9]+%' | head -1 | tr -d '%'") as integer
		if pct ≤ threshold then
			do shell script "launchctl bootout gui/$(id -u)/" & agentLabel & " 2>/dev/null; pkill -x channels-dvr; true"
			display notification "Battery at " & pct & "%, Channels DVR stopped" with title "Battery Guard"
			quit
		end if
	end if
	return 60 -- check every 60s
end idle
-- Save as Application with "Stay open after run handler" checked
property threshold : 50
property agentLabel : "com.getchannels.dvr"

on idle
	set pct to (do shell script "pmset -g batt | grep -Eo '[0-9]+%' | head -1 | tr -d '%'") as integer
	if pct ≥ threshold then
		set isRunning to (do shell script "pgrep -x channels-dvr >/dev/null && echo yes || echo no")
		if isRunning is "no" then
			do shell script "launchctl bootstrap gui/$(id -u) \"$HOME/Library/LaunchAgents/" & agentLabel & ".plist\" 2>/dev/null; launchctl kickstart gui/$(id -u)/" & agentLabel & " 2>/dev/null; true"
			display notification "Battery at " & pct & "%, Channels DVR restarted" with title "Battery Guard"
		end if
		quit
	end if
	return 60 -- check every 60s
end idle

Something like this maybe. Unload the plst and then kill the process for good measure if the battery threshold hits 25%, and then if it reaches 50%, reload the plist.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.