检测是否在启动状态下运行 [英] Detect if running under launchd

查看:164
本文介绍了检测是否在启动状态下运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测到我在Mac OS X上启动时正在作为LaunchAgent运行?

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

这是一个程序(用Go编写),当需要执行该程序时需要重新启动.如果它是从终端或init脚本启动的,则需要自己完成.如果在服务管理器下运行,则不应尝试重新启动,而应退出并由管理器重新启动.

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.

例如,在Solaris SMF下运行时,设置了环境变量SMF_FMRI,但是在启动时看不到类似的东西.

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.

(这不是类似听起来的问题的重复如何检查launchd是否已启动脚本?)

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

推荐答案

获取您的父进程ID(PPID),并将其与启动的PID进行比较.

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

在屏幕抓取中,您可以看到launchd's PID为41840(红色),并且mdflagwriterdistnotedcfprefsd(蓝色)的PPID与该数字匹配,因此它们是由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.

这篇关于检测是否在启动状态下运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆