使用brew services管理服务

 mac  使用brew services管理服务已关闭评论
11月 122019
 

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

以brew install redis后自启动redis为例,使用launchctl启动过程如下:

  1. ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
  2. launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
  3. brew services restart redis

常用命令

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  # 清除已卸载应用的无用的配置

 

配置文件目录

/Library/LaunchDaemons # 开机自启,需要sudo

~/Library/LaunchAgents # 用户登录后自启

 

 

举例:homebrew.mxcl.redis.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>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>Label</key>
<string>homebrew.mxcl.redis</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/redis/bin/redis-server</string>
<string>/usr/local/etc/redis.conf</string>
<string>–daemonize no</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/usr/local/var</string>
<key>StandardErrorPath</key>
<string>/usr/local/var/log/redis.log</string>
<key>StandardOutPath</key>
<string>/usr/local/var/log/redis.log</string>
</dict>
</plist>

 

官网参考内容:
https://github.com/Homebrew/homebrew-services