Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to monitor a process on OS X?

I am looking for a way to monitor the state of one of my applications on OS X. There's a number of components that I need to monitor such as the status of various communication channels. If they go down, the monitoring process should be able to warn the user both on screen and via a push notification.

XPC services look promising, but if the app crashes, I presume this will take out the service as well, or am I mistaken?

My preferred solution is something which would also monitor for unexpected termination, and restart the app if it happens.

What is the best way to do this?

like image 851
user2002649 Avatar asked Oct 06 '22 06:10

user2002649


2 Answers

I think monitoring communication channels, etc. must be done by the each specific components (processes). And if the unexpected error occur that component should exit immediately to ensure proper cleanup.

For processe monitoring, below Apple Technical Q&A document will be really helpful:
Technical Note TN2050: Observing Process Lifetimes Without Polling

like image 199
9dan Avatar answered Oct 10 '22 13:10

9dan


You could write an app which starts your main application as a child process, and waits for it to exit. It could check the exit code, and then react according to your needs.

This approach is explained here: https://stackoverflow.com/a/78095/785411

To fork() some monitoring process to run your main application as a child process, this is explained here: https://stackoverflow.com/a/4327062/785411

like image 31
Chris McGrath Avatar answered Oct 10 '22 13:10

Chris McGrath