BlueXIII's Blog

热爱技术,持续学习

0%

使用brew services管理服务

简介

官网:
https://github.com/Homebrew/homebrew-services

macOS使用launchctl命令加载开机自动运行的服务,brew service可以简化lauchctl的操作。

以MySQL为例,使用launchctl启动:

1
2
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

如使用brew service可以简化为:

1
brew services start mysql

常用命令

1
2
3
4
5
6
brew services list  # 查看使用brew安装的服务列表
brew services run formula|--all # 启动服务(仅启动不注册)
brew services start formula|--all # 启动服务,并注册
brew services stop formula|--all # 停止服务,并取消注册
brew services restart formula|--all # 重启服务,并注册
brew services cleanup # 清除已卸载应用的无用的配置

配置文件目录

1
2
/Library/LaunchDaemons # 开机自启,需要sudo
~/Library/LaunchAgents # 用户登录后自启

以homebrew.mxcl.kafka.plist为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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>Label</key>
<string>homebrew.mxcl.kafka</string>
<key>WorkingDirectory</key>
<string>/usr/local</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/kafka/bin/kafka-server-start</string>
<string>/usr/local/etc/kafka/server.properties</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardErrorPath</key>
<string>/usr/local/var/log/kafka/kafka_output.log</string>
<key>StandardOutPath</key>
<string>/usr/local/var/log/kafka/kafka_output.log</string>
</dict>
</plist>

在这里可以找到服务路径、启动参数、日志路径等