问题描述:很长时间的问题是可以看到一个 mongo 进程高占用,CPU占用率 始终接近 100(即使通过 brew services stop mongodb-community 关闭服务后也是如此。

(临时)解决

从 monitor 中看到该名为 mongo 的进程的父进程是 launchd ,也就是开机启动?(存疑)【因为,实际上,通过 brew services run mongodb-community 启动的服务是 mongod,并不会出现占用高的问题。】

将该进程手动结束后即可。

补充:通过 iTerm 连接 mongo 也会生成名为 mongo 的进程,但其父进程为 zsh 也不会出现高占用的问题。

launchd 与启动管理

关于 launchd 进程

参见 Mac系统的launchd、守护进程daemon

Mac的守护进程目录有以下几处:

1
2
3
4
5
~/Library/LaunchAgents  # 用户的进程
/Library/LaunchAgents # 管理员设置的用户进程
/Library/LaunchDaemons # 管理员提供的系统守护进程
/System/Library/LaunchAgents # Mac操作系统提供的用户进程
/System/Library/LaunchDaemons # Mac操作系统提供的系统守护进程

另:/Library/StartupItems 这个目录下也有可能存在开机启动项目的配置 以上是launchd的相关配置的存放目录,可以看到,一般我们个人编写的守护进程,都应该放到~/Library/LaunchAgents目录里面。

launchctl 启动进程控制

参见 控制macOS的开机启动

上一节几个目录下存放的是各项服务的配置文件,而如何管理启动服务?

1
2
3
4
5
6
7
8
9
10
11
12
# 查看所有自启动项
launchctl list

# 添加自启动项
//launchctl工具提供了一系列接口方便使用launchd程序
launchctl load ~/Library/LaunchAgents/aria2.plist

# 启动自启动项
launchctl start aria2

# 删除自启动项
launchctl unload ~/Library/LaunchAgents/aria2.plist

~/Library/LaunchAgents/ 文件夹?

之前看到 Github 上有通过删除 ~/Library/LaunchAgents/ 文件夹下相应文件来删除自启动项的。但按照上文的说法,似乎这里仅仅是个配置文件?

以 ElasticSearch 为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ cat homebrew.mxcl.elasticsearch.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<false/>
<key>Label</key>
<string>homebrew.mxcl.elasticsearch</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/elasticsearch/bin/elasticsearch</string>
</array>
<key>EnvironmentVariables</key>
<dict>
</dict>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/usr/local/var</string>
<key>StandardErrorPath</key>
<string>/usr/local/var/log/elasticsearch.log</string>
<key>StandardOutPath</key>
<string>/usr/local/var/log/elasticsearch.log</string>
</dict>
</plist>

正确的使用,似乎应该通过上面的 launchctl load 来进行注册? 【当然,删除应该也是可以的;另外,brew services start 有所不一样。 我好像将该文件夹下的 homebrew.mxcl.mongodb-community 删除了,但通过 brew services start 仍然会生成自启动项 —— 应该是直接关联了模板文件,见下。】

添加自启动项

看到这个问题 https://stackoverflow.com/questions/8014500/macosx-autostart-mysql-on-boot 里面提到了如何配置 mysql 的自动启 —— 对于 brew 安装的应该是不用的,因此也适用于下载包来进行安装的软件

1
2
3
mkdir -p ~/Library/LaunchAgents
cp `brew --prefix mysql`/*mysql*.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/*mysql*.plist

可见,就是将 mysql 本身的自启动项配置文件复制到了 ~/Library/LaunchAgents 下,然后使用 launchctl load 添加配置。

brew --prefix 查看安装位置

注意到,上面用到了 brew --prefix 查看命令 brew --prefix --help:「Display Homebrew's install path.」

例如 brew --prefix mongodb-community 的结果就是 /usr/local/opt/mongodb-community

查看/搜索自启动项

launchctl list 命令来查看。例如,可用

1
launchctl list | grep mongo

来搜索 mongo 启动项。

返回的一个结果 33814 0 homebrew.mxcl.mongodb-community 的配置文件在 ~/Library/LaunchAgents 目录下。

补充

这个关于 brew 管理 Redis 的教程也列出了一些相关命令,包括 launchctl 的使用,redis 的配置文件 /usr/local/etc/redis.conf 等。

brew 使用

brew services

以下是 brew services -h 的结果,可知

  • brew services start 会启动服务并且设置开机自启
  • brew services run 则单纯启动,而不会设置开机自启
  • brew services stop 会关闭服务并且取消开机自启
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$ brew services -h
Usage: brew services [subcommand]

Manage background services with macOS' launchctl(1) daemon manager.

If sudo is passed, operate on /Library/LaunchDaemons (started at boot).
Otherwise, operate on ~/Library/LaunchAgents (started at login).

[sudo] brew services [list]:
List all managed services for the current user (or root).

[sudo] brew services run (formula|--all):
Run the service formula without registering to launch at login (or boot).

[sudo] brew services start (formula|--all):
Start the service formula immediately and register it to launch at login
(or boot).

[sudo] brew services stop (formula|--all):
Stop the service formula immediately and unregister it from launching at
login (or boot).

[sudo] brew services restart (formula|--all):
Stop (if necessary) and start the service formula immediately and register
it to launch at login (or boot).

[sudo] brew services cleanup:
Remove all unused services.

--all Run subcommand on all services.
-d, --debug Display any debugging information.
-q, --quiet Make some output more quiet.
-v, --verbose Make some output more verbose.
-h, --help Show this message.

brew info

描述是 「Display brief statistics for your Homebrew installation.」非常有用! 例如下面 brew info mongodb-community 显示了启动 mongo 服务的命令; 另外,对于如未安装的 musql 服务也可显示相关信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ brew info mongodb-community
mongodb/brew/mongodb-community: stable 4.4.4
High-performance, schema-free, document-oriented database
https://www.mongodb.com/
/usr/local/Cellar/mongodb-community/4.4.3 (11 files, 156.8MB) *
Built from source on 2021-03-18 at 14:34:09
From: https://github.com/mongodb/homebrew-brew/blob/HEAD/Formula/mongodb-community.rb
==> Dependencies
Recommended: mongodb-database-tools ✘
==> Options
--without-mongodb-database-tools
Build without mongodb-database-tools support
==> Caveats
To have launchd start mongodb/brew/mongodb-community now and restart at login:
brew services start mongodb/brew/mongodb-community
Or, if you don't want/need a background service you can just run:
mongod --config /usr/local/etc/mongod.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ brew info mysql
mysql: stable 8.0.23 (bottled)
Open source relational database management system
https://dev.mysql.com/doc/refman/8.0/en/
Conflicts with:
mariadb (because mysql, mariadb, and percona install the same binaries)
percona-server (because mysql, mariadb, and percona install the same binaries)
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/mysql.rb
License: GPL-2.0-only
==> Dependencies
Build: cmake ✘
Required: openssl@1.1 ✘, protobuf ✘
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
mysql -uroot

To have launchd start mysql now and restart at login:
brew services start mysql
Or, if you don't want/need a background service you can just run:
mysql.server start

mongo 任务管理

db.currentOp() 可查看在执行的任务?而 db.killOp(290014) 的参数为 opid 的值。 与本次问题无关。