macOS终端实用工具与Brew服务使用指南
立即解锁
发布时间: 2025-09-12 01:59:34 阅读量: 10 订阅数: 31 AIGC 


Mac终端高手进阶指南
### macOS 终端实用工具与 Brew 服务使用指南
#### 1. Brew 服务管理
Brew 具备管理后台任务的能力。若你首次使用 Brew 服务,Brew 会先进行安装:
```bash
% brew services
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 24, done.
remote: Counting objects: 100% (24/24), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 24 (delta 0), reused 16 (delta 0), pack-reused 0
Unpacking objects: 100% (24/24), 11.43 KiB | 688.00 KiB/s, done.
Tapped 1 command (62 files, 66.8KB).
Usage: brew services subcommand:
Manage background services with macOS' launchctl(1) daemon manager
```
安装完成后,使用 `--help` 可获取完整帮助信息:
```bash
% brew services --help
```
若已安装 Brew 服务,该命令会显示可用服务列表。
##### 1.1 列出服务
Brew 服务有多个子命令,其中 `list` 用于列出服务,若未指定子命令,默认使用 `list`:
```bash
% brew services list
Name Status User Plist
```
初始时可能无服务显示,可安装 DNS 解析器 `unbound`:
```bash
% brew install unbound
==> Installing unbound
==> Pouring unbound-1.10.1.catalina.bottle.tar.gz
==> Caveats
To have launchd start unbound now and restart at startup:
sudo brew services start unbound
==> Summary
🍺 /usr/local/Cellar/unbound/1.10.1: 57 files, 5MB
```
再次列出服务,可看到 `unbound` 服务:
```bash
% brew services list
Name Status User Plist
unbound stopped
```
各列描述如下表:
| 标题 | 描述 |
| --- | --- |
| Name | 服务名称 |
| Status | 服务状态,如已启动、已停止 |
| User | 服务运行的用户,具有该用户的相同权限 |
| Plist | 包含定义如何启动此服务的文件路径,通常无需编辑 |
需注意,在服务启动前,`User` 和 `Plist` 列无内容。此外,若服务崩溃,Brew 可能仍显示其正在运行。
##### 1.2 启动服务
以当前用户身份启动服务,使用 `start` 命令和服务名称:
```bash
% brew services start unbound
Warning: unbound must be run as root to start at system startup!
==> Successfully started `unbound` (label: homebrew.mxcl.unbound)
```
`unbound` 设计为以 root 身份运行,但以当前用户身份运行也不会造成损害。启动后,再次列出服务,`User` 和 `Plist` 列将显示内容:
```bash
% brew services list
Name Status User Plist
unbound started danielplatt /Users/danielplatt/Library/LaunchAgents/homebrew.mxcl.unbound.plist
```
##### 1.3 停止服务
若不想让服务继续运行,可使用 `stop` 命令停止服务:
```bash
% brew services stop unbound
Stopping `unbound`... (might take a while)
```
此操作在升级应用程序的主要版本时很有用,可同时保留两个版本。
##### 1.4 重启服务
当更改服务配置时,可能需要重启服务,使用 `restart` 命令:
```bash
% brew services restart unbound
Stopping `unbound`... (might take a while)
==> Successfully stopped `unbound` (label: homebrew.mxcl.unbound)
==> Successfully started `unbound` (label: homebrew.mxcl.unbound)
```
`restart` 命令是 `stop` 和 `start` 的快捷方式。
##### 1.5 使用 sudo
有时需要以 root 身份运行命令以获取更多权限,例如启动端口小于 1024 的 Web 服务器。以 root 身份运行 Brew 服务,需在命令前加 `sudo`:
```bash
% sudo brew services start <service>
```
若使用 `sudo` 启动服务,停止时也需使用 `sudo`,否则无权限停止服务。
#### 2. 实用工具
##### 2.1 archey 工具
`archey` 工具可显示简单的启动屏幕和复古苹果标志,并提供计算机信息、登录用户、主机名、正常运行时间等信息:
```bash
% brew install archey
```
可将 `archey` 添加到 `.zshrc` 文件中,每次打开终端时显示信息。
##### 2.2 macOS 的 Linux 实用工具
macOS 自带的许多命令源自 BSD,对应的 Linux 命令支持更多参数且更易读。例如,macOS 的 `tail` 命令使用 `-n` 指定输出行数,而 Linux 版本还支持 `--lines`。可通过安装 `coreutils` 来获取这些 Linux 命令:
```bash
% brew install coreutils
==> Caveats
Commands also provided by macOS have been installed with the prefix "g".
If you need to use these commands with their normal names, you can add a "gnubin" directory to your PATH from your bashrc like:
PATH="/usr/lo
```
0
0
复制全文
相关推荐









