gpt4 book ai didi

macos - 如何从 shell 脚本检查 MAC OS X 中启动的进程是否已加载?

转载 作者:行者123 更新时间:2023-12-01 22:35:58 33 4
gpt4 key购买 nike

我有一个launchd进程要卸载,我使用的命令是

launchctl 卸载/System/Library/LaunchDaemons/costomscript.plist

如果进程已经加载,它就可以正常工作。但是,如果它没有加载并且我执行了命令,它会给出一条消息,说没有加载这样的进程。所以我需要检查一下,如果 .plist 文件当前已加载,则只应卸载它,否则不应卸载。

我怎样才能做到这一点..请帮忙。谢谢!!

最佳答案

您可以使用launchctl获取正在运行的进程的信息。

一种可能是使用 launchctl list 命令查询 launchd

list [-x] [label]

With no arguments, list all of the jobs loaded into launchd in three columns. The first column displays the PID of the job if it is running. The second column displays the last exit status of the job. If the number in this column is negative, it represents the negative of the signal which killed the job. Thus, "-15" would indicate that the job was terminated with SIGTERM. The third column is the job's label.

如果您的 plist 已加载,则应该列出它,否则不会。另外,第一列包含进程的 pid,因此您可以检查进程是否正在运行,例如:

$ launchctl list |grep myprocess
600 0 org.example.myprocess.1234

还有 launchctl print 命令,可以提供有关进程的详细输出。检查是否可以使用它。

print domain-target | service-target

Prints information about the specified service or domain. Domain output includes various properties about the domain as well as a list of services and endpoints in the domain with state pertaining to each. Service output includes various properties of the service, including information about its origin on-disk, its current state, execution context, and last exit status.

例如:

$ launchctl print gui/501/org.example.myprocess.1234 | grep state
state = running

关于macos - 如何从 shell 脚本检查 MAC OS X 中启动的进程是否已加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29815537/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com