美文网首页
brew安装服务开机自启动

brew安装服务开机自启动

作者: leejnull | 来源:发表于2020-02-17 19:09 被阅读0次

    通过 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/

    相关文章

      网友评论

          本文标题:brew安装服务开机自启动

          本文链接:https://www.haomeiwen.com/subject/rxjifhtx.html