macos 如何在 Mac 中检查进程/守护进程的状态(运行/停止)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3430402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check the status(running/stopped) of a process/daemon in Mac?
提问by user389547
In Linux, we have the command /etc/init.d/process_name status
, this will give whether the process/daemon is running or stopped.
在 Linux 中,我们有命令/etc/init.d/process_name status
,这将给出进程/守护程序是正在运行还是已停止。
For Example In Ubuntu:
例如在 Ubuntu 中:
root@Ubu91032b-Bassu:~# /etc/init.d/ssh status
* sshd is running
root@Ubu91032b-Bassu:~#
My question is, is there any command (like above) in Mac to check the status of a daemon/process?
我的问题是,Mac 中是否有任何命令(如上)来检查守护进程/进程的状态?
回答by Christopher Creutzig
The documented “modern” way would, I believe, be to ask launchctl
, the controlling tool for launchd
, which Apple uses to replace init
, inetd
, crond
and a bit more:
我相信,记录在案的“现代”方式是询问launchctl
、 的控制工具launchd
,Apple 用它来替换init
、inetd
,crond
以及更多:
~> sudo launchctl list | grep ssh
41032 - 0x100502860.anonymous.sshd
- 0 com.openssh.sshd
回答by chiggsy
Yes there is a way to do this within the launchd/launchctl paradigm:
是的,有一种方法可以在 launchd/launchctl 范式中做到这一点:
sudo launchctl bslist
will give you output of all loaded launchd processes, with
会给你所有加载的launchd进程的输出,
A for active. It's running
A 表示活跃。它正在运行
I for inactive. It's not supposed to run. It should not run on it's own, and I hope you notice how my tone is not definitive. But it's not supposed to surprise you, I should mean off.
我为不活跃。它不应该运行。它不应该自己运行,我希望你注意到我的语气不是明确的。但它不应该让你感到惊讶,我应该是故意的。
D for on demand. Not running now, but could be, since it could have started at any time.
D 按需。现在不运行,但可以运行,因为它可以随时开始。
Also, if you want a treelike structure, so you can see which process fathered what:
另外,如果你想要一个树状结构,那么你可以看到哪个进程产生了什么:
sudo launchctl bstree
You'll get
你会得到
A com.apple.windowserver.active
D com.apple.DirectoryService.localonly
com.apple.metadata.mds[46].subset.109 (Explicit Subset)/
D com.apple.Spotlight.ImporterWorker.89
D com.apple.Spotlight.ImporterWorker.i386.89
A com.apple.Spotlight.ImporterWorker.501
D com.apple.Spotlight.SyncScanWorker
Which is a tree of the processes and their states.
这是进程及其状态的树。
If you are anything like me, you'll be wanting to use some stuff from herebecause you might find some peculiar things when you look.
如果你和我一样,你会想要使用这里的一些东西,因为当你看的时候你可能会发现一些奇怪的东西。
回答by Scott Robert Schreckengaust
To toggle remote login use the "System Preferences" => "Sharing" => "Remote Login" via the user interface to enable SSH (see http://support.apple.com/kb/PH13759for more).
要切换远程登录,请通过用户界面使用“系统偏好设置”=>“共享”=>“远程登录”以启用 SSH(有关更多信息,请参阅http://support.apple.com/kb/PH13759)。
Remote Login via SSH Disabled (Unchecked):
通过 SSH 远程登录已禁用(未选中):
$ sudo launchctl list com.openssh.sshd
launchctl list returned unknown response
Remote Login via SSH Enabled (Checked):
通过 SSH 远程登录已启用(选中):
$ sudo launchctl list com.openssh.sshd
{
"Label" = "com.openssh.sshd";
"LimitLoadToSessionType" = "System";
"OnDemand" = true;
"LastExitStatus" = 0;
"TimeOut" = 30;
"Program" = "/usr/libexec/sshd-keygen-wrapper";
"StandardErrorPath" = "/dev/null";
"ProgramArguments" = (
"/usr/sbin/sshd";
"-i";
);
"inetdCompatibility" = {
"Wait" = false;
};
"Sockets" = {
"Listeners" = (
file-descriptor-object;
file-descriptor-object;
);
};
};
回答by sakra
You can use the serviceshell command for that purpose:
为此,您可以使用serviceshell 命令:
bash-3.2$ service
usage: service service command
service --list
service --test-if-configured-on service
service --test-if-available service
bash-3.2$ service --test-if-configured-on ssh && echo "SSH running"
SSH running
The command has been removed in OS X versions newer than 10.6 (Snow Leopard). Use launchctl
instead.
该命令已在高于 10.6 (Snow Leopard) 的 OS X 版本中删除。使用launchctl
来代替。