Detect if running under launchd

倾然丶 夕夏残阳落幕 提交于 2019-12-13 02:29:33

问题


How do I detect that I'm running as a LaunchAgent under launchd on Mac OS X?

This is for a program (written in Go) that needs to restart when told to do so. If it's been started from a terminal or init script, it needs to do this by itself. If running under a service manager it should not attempt to restart but simply exit and be restarted by the manager.

For example, when running under Solaris SMF the environment variable SMF_FMRI is set, but I see nothing similar in launchd.

There are obvious workarounds (require a command line flag to be used or an environment variable to be set) but I'm interested in an "automatic" solution.

(This is not a duplicate of the similar sounding question How to check if launchd has started the script?)


回答1:


Get your Parent Process Id (PPID) and compare it with the PID of launchd.

In the screen grab, you can see launchd's PID is 41840 (in red) and the PPIDs of mdflagwriter, distnoted and cfprefsd (in blue) match that number, so they were launched by launchd.



来源:https://stackoverflow.com/questions/22344967/detect-if-running-under-launchd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!