Can we disable KeepAlive temporarily for launchctl?

I have a process [command line cpp application] which i want to run always such as it should relaunch after a crash, after device startup etc.

I created a launchd Property List File with KeepAlive true and placed under /Library/LaunchDaemons.

Problem Statements:

I have a bash script to start and stop this process.

start using: launchctl bootstrap.

stop involve these two steps:

  1. send SIGTERM signal and wait untill process stops after doing some cleanups
  2. launchctl bootout [It doesn't sends SIGTERM]

during steps 1 - Process is getting stop, but also getting immediate relaunch by launchctl

during step 2 - it getting stop again.

is there a proper way so that we can disable KeepAlive temporarily so that process will not launch during step 1? or suggest other ways to handle this?

Answered by DTS Engineer in 814211022

So, you want to get the SIGTERM as part of the stopping process? If so, start a transaction in your daemon. That’ll mark it as ‘busy’, so the stop will send you a SIGTERM before the inevitable SIGKILL.

Annoyingly, there isn’t a better public API for starting a transaction than xpc_transaction_begin (r. 37489913).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

So, you want to get the SIGTERM as part of the stopping process? If so, start a transaction in your daemon. That’ll mark it as ‘busy’, so the stop will send you a SIGTERM before the inevitable SIGKILL.

Annoyingly, there isn’t a better public API for starting a transaction than xpc_transaction_begin (r. 37489913).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks much @DTS Engineer.

During KeepAlive true, if process crashes after launch, what best can be done to minimise crash loop or at-least minimising system instability or excessive resource caused by crash loop?

launchd automatically guards against crash loops by throttling the rate at which it’ll start a job. IIRC the throttle period is 10 seconds. It’d be easy for you to work out the current value, but keep in mind that this is an implementation detail.

Beyond that, I don’t have any great suggestions. Generally I recommend that folks not force a daemon to stay alive — rather, it should launch on demand — but that’s not an option for an ES client.

You could create another daemon that watches for this problem, but what’s that gonna do if it detects it? You probably aren’t allowed to turn off your ES client. And you could force a system restart, but it’s not hard to imagine that ending very badly.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Can we disable KeepAlive temporarily for launchctl?
 
 
Q