brew安装服务开机自启动

通过 brew services start

当我们通过 brew install 安装第三方库比如 redis 时,安装完会有这样一段提示

redis: stable 5.0.7 (bottled), HEAD
Persistent key-value database, with built-in net interface
https://redis.io/
/Users/lijun/homebrew/Cellar/redis/5.0.7 (13 files, 3.1MB) *
  Poured from bottle on 2020-02-17 at 18:31:08
From: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git/Formula/redis.rb
==> Options
--HEAD
    Install HEAD version
==> Caveats
To have launchd start redis now and restart at login:
  brew services start redis
Or, if you don't want/need a background service you can just run:
  redis-server /Users/lijun/homebrew/etc/redis.conf

其中已经说明了为了现在启动和登录时重新启动,请执行

brew services start redis

使用 services 指令,请先安装

brew tap gapple/services

安装完成后使用

$ brew services start mysql
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)

那么 mysql 服务就会在后台启动,并且电脑开机重新登录也会自启动

原理是利用 launchctl 命令

在 OS X 中,服务本身存储在 .plist 文件中,这些文件的位置一般在 ~/Library/LaunchAgents 中。可以使用 launchctl load PATH_TO_LIST 和 launchctl unloadPATH_TO_LIST 命令来加载和卸载他们,从而达到开机自动运行的目的。

所有通过 brew install 安装的库文件都在 homebrew 的安装文件夹下,通过软连接,将库的 plist 文件链接到 ~/Library/LaunchAgents 中

ln -sfv homebrew所在文件夹/Cellar/redis/5.0.7/homebrew.mxcl.redis.plist ~/Library/LaunchAgents

然后在加载他们

launchctl load homebrew所在文件夹/Cellar/redis/5.0.7/homebrew.mxcl.redis.plist

卸载

launchctl unload homebrew所在文件夹/Cellar/redis/5.0.7/homebrew.mxcl.redis.plist

brew services start 其实最后也就是执行了上述的命令。

最后

如果卸载了 redis 但是 homebrew 没有把 plist 文件删除的话,可以

brew services cleanup

当执行brew services start xxx提示

Error: No such file or directory

可能是放入LaunchAgents的文件有问题,我们可以直接把plist文件拷贝进来

cp homebrew所在文件夹/Cellar/redis/5.0.7/homebrew.mxcl.redis.plist ~/Library/LaunchAgents

补充

当我们使用brew services命令的时候,很有可能出现

Error: Unknown command: services

这时候就要安装services命令

> curl -o /usr/local/bin/brew-services.rb https://gist.githubusercontent.com/lwe/766293/raw/75a7907004bbff0eb3b072d1d951be2cfe7e5020/brew-services.rb
> chmod +x /usr/local/bin/brew-services.rb
> brew services help
usage: [sudo] brew services [--help] <command> [<formula>]

Small wrapper around `launchctl` for supported formulas, commands available:
   cleanup Get rid of stale services and unused plists
   list    List all services managed by `brew services`
   restart Gracefully restart selected service
   start   Start selected service
   stop    Stop selected service

Options, sudo and paths:

  sudo   When run as root, operates on /Library/LaunchDaemons (run at boot!)
  Run at boot:  /Library/LaunchDaemons
  Run at login: /Users/ian/Library/LaunchAgents

但是当安装完之后使用如果出现下面的问题

Error: undefined method `resolve_alias' for Formula:Class
/usr/local/bin/brew-services.rb:154:in `service'
/usr/local/bin/brew-services.rb:203:in `check'
/usr/local/bin/brew-services.rb:193:in `run!'
/usr/local/bin/brew-services.rb:397:in `<top (required)>'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/usr/local/Homebrew/Library/Homebrew/utils.rb:20:in `require?'
/usr/local/Homebrew/Library/Homebrew/brew.rb:106:in `<main>'

请尝试下面的方法

Hey there,

I've been faced this same issue and I figured out this way.

[#1](https://github.com/Homebrew/legacy-homebrew/issues/1)
I got this message when I ran brew doctor :
Warning: You have external commands with conflicting names.
Found command `brew-services` in following places:
/usr/local/bin/brew-services.rb
/usr/local/Library/Taps/homebrew/homebrew-services/cmd/brew-services.rb

I've been getting this message when I tried to check brew services available

$ brew services list
Warning: No user-space services controlled by `brew services` running...

[#2](https://github.com/Homebrew/legacy-homebrew/issues/2)
So, I decided to remove the first installation that was out of Library/Taps directories.
rm -rf /usr/local/bin/brew-services.rb

[#3](https://github.com/Homebrew/legacy-homebrew/issues/3)
Now, to be sure that everything is okay, I ran
brew update && brew upgrade

[#4](https://github.com/Homebrew/legacy-homebrew/issues/4) Finally, brew services is working fine now.

$ brew services list
Name Status User Plist
dnsmasq started root /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
httpd22 started root /Library/LaunchDaemons/homebrew.mxcl.httpd22.plist
mysql started luciano /Users/luciano/Library/LaunchAgents/homebrew.mxcl.mysql.plist
php56 started luciano /Users/luciano/Library/LaunchAgents/homebrew.mxcl.php56.plist

\o/

1.删除/usr/local/bin/brew-services.rb文件
2.执行 brew update 和 brew upgrade 命令
3.brew services list 查看是否可以正确使用

来自 https://leejnull.github.io/2020/02/17/2020-02-17-01/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,688评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,559评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,749评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,581评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,741评论 3 271
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,684评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,122评论 2 292
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,847评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,441评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,939评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,333评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,783评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,275评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,830评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,444评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,553评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,618评论 2 249

推荐阅读更多精彩内容