MacOS添加开机启动

6,028 阅读5分钟

关于开机启动

开机启动某一个服务,是指服务器因意外宕机重启后一个服务可以自行启动对外正常提供业务服务。对于开机服务自动启动不需要认为干预,可以防止业务在第一时间恢复正常运行减少客户投诉、理赔,运维避免背锅、跳坑无法自拔……各系统开机启动服务的方式都不一样,这里介绍Unix系统开机启动的方式。

RedHat/CentOS/

chkconfig

  • 文件存放位置: /etc/init.d/
  • 启动方式: chkconfig --add mysql --level 2345

systemd

  • 文件存放位置:/usr/lib/systemd/system/
  • 启动方式:systemctl enable mysql

rc.local

  • 内容写入:/etc/init.d/rc.local

Ubuntu/Debian/Deepin(16/18版本之后)

chkconfig

  • 文件存放位置: /etc/init.d/
  • 启动方式: chkconfig --add mysql --level 2345

systemd

  • 文件存放位置:/usr/lib/systemd/system/
  • 启动方式:systemctl enable mysql

rc.local

  • 内容写入:/etc/init.d/rc.local 习惯了使用以上系统添加开机启动的方式,对于切换到 macos 的用户来说想要添加开机服务该怎么做呢?当按照以往经验去设置的时候发现都是同样的类 Unix 系统,差别居然如此之大。那如何为 macOS 添加开机启动方式呢?下面将做详细的介绍

MacOS[FreeBSD]

使用launchd方式添加启动项

Apple 要求开发者使用 launchd 来添加开机启动服务,这个 launchd 会用两个进程来启动服务,分别是daemon一个是agent。前者处理系统开机后并启动服务,后者处理用户登录后并启动服务,他们的配置文件是一个符合 xml 规范的plist文本文档来实现的,该plist位于以下目录,各目录决定了其启动的先后和拥有的权限:

  • ~/Library/LaunchAgents //特定用户登录后以当前用户启动,第三方程序一般都放这里

  • /Library/LaunchAgents //任一用户登录后以当前用户启动,管理员使用

  • /System/Library/LaunchAgents //系统组件,任一用户登录后以当前用户启动

  • /Library/LaunchDaemons //系统装载时以root用户启动,管理员使用

  • /System/Library/LaunchDaemons //系统组件,系统装载时以root用户启动 我们这里以redis、leanote两个应用为例。

  • Redis-server

    • 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>
              <true/>
              <key>Label</key>
              <string>com.redis.redis-server</string>
              <key>Program</key>
              <string>/usr/local/bin/redis-server</string>
              <key>ProgramArguments</key>
              <array>
                      <string>/usr/local/bin/redis-server</string>
                      <string>/usr/local/etc/redis/redis.conf</string>
              </array>
              <key>RunAtLoad</key>
              <true/>
              <key>StandardErrorPath</key>
              <string>/usr/local/var/log/launchd_redis.log</string>
              <key>StartInterval</key>
              <integer>3</integer>
              <key>WorkingDirectory</key>
              <string>/usr/local/var/lib/redis</string>
      </dict>
      </plist>
      

      准备好以上文件后,将 plist 文件放到/Library/LaunchDaemons/com.apple.redis-server.plist,最后就是设置开机启动:

      sudo launchctl load -w /Library/LaunchDaemons/com.apple.redis-server.plist
      

      注意:(1)如果执行了之后找不到 redis 进程可以执行下面的这个命令sudo launchctl start com.redis.redis-server(2)Label 这里不要写 com.apple.*这里可能会进行校验,导致服务无法正常启动

    • leanote(蚂蚁笔记)

      • leanote 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>Disabled</key>
                <false/>
                <key>KeepAlive</key>
                <dict>
                        <key>SuccessfulExit</key>
                        <false/>
                        <key>Crashed</key>
                        <true/>
                </dict>
                <key>EnvironmentVariables</key>
                <dict>
                        <key>GO111MODULE</key>
                        <string>on</string>
                        <key>PATH</key>
                        <string>/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin</string>
                        <key>GOROOT</key>
                        <string>/usr/local/Cellar/go/1.16.4/libexec</string>
                        <key>GOPATH</key>
                        <string>/Users/miyamoto/go</string>
                </dict>
                <key>Label</key>
                <string>com.leanote.server</string>
                <key>ProcessType</key>
                <string>Background</string>
                <key>Program</key>
                <string>/usr/local/bin/revel</string>
                <key>ProgramArguments</key>
                <array>
                        <string>/usr/local/bin/revel</string>
                        <string>run</string>
                        <string>--run-mode</string>
                        <string>prod</string>
                        <string>--application-path</string>
                        <string>/usr/local/leanote_source</string>
                </array>
                <key>RunAtLoad</key>
                <true/>
                <key>StandardErrorPath</key>
                <string>/usr/local/var/log/launchd_leanote.log</string>
                <key>WorkingDirectory</key>
                <string>/usr/local/leanote_source</string>
        </dict>
        </plist>
        

        准备好以上文件后,将 plist 文件放到/Library/LaunchDaemons/com.apple.leanote.plist,最后就是设置开机启动:

        sudo launchctl load -w /Library/LaunchDaemons/com.apple.leanote.plist
        

        注意:如果执行了之后找不到 leanote 进程可以执行下面的这个命令sudo launchctl start com.leanote.server

        如果需要添加其他的开机设置,修改上面的部分内容就可以了。如果需要查看更多的参数,请使用 man launchd.plist

使用登录项

操作步骤:系统偏好设置-->用户与群组-->登录项-->添加应用

不值一提的踩坑

    <key>Program</key>
    <string>/usr/local/bin/redis-server</string>
    <key>ProgramArguments</key>
    <array>
            <string>/usr/local/etc/redis/redis.conf</string>
    </array>

上面这部分如果只有配置文件,启动起来的服务并不是按照配置文件中的配置来启动的,加载服务的时候日志里面提示找不到配置文件,使用redeis-cli客户端连接进去执行了一下config get dir,显示为/,明明在启动文件里面加了ProgramArguments启动参数,可输出的日志提示需要手动指定配置文件.具体内容如下:

3189:C 11 Apr 2020 22:58:13.262 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=3189, just started
3189:C 11 Apr 2020 22:58:13.262 # Warning: no config file specified, using the default config. In order to specify a config file use /usr/local/etc/redis/redis.conf /path/to/redis.conf

根据redis上面的提示信息来看启动的时候使用的默认配置而不是使用ProgramArguments指定的启动参数来启动服务,这无法使用自定义的配置文件

2020-04-11T23:36:14.347+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-04-11T23:36:14.357+0800 I CONTROL  [initandlisten] MongoDB starting : pid=4483 port=27017 dbpath=/data/db 64-bit host=miyamotodembp
2020-04-11T23:36:14.357+0800 I CONTROL  [initandlisten] db version v4.0.9
2020-04-11T23:36:14.357+0800 I CONTROL  [initandlisten] git version: fc525e2d9b0e4bceff5c2201457e564362909765
2020-04-11T23:36:14.357+0800 I CONTROL  [initandlisten] allocator: system
2020-04-11T23:36:14.357+0800 I CONTROL  [initandlisten] modules: none
2020-04-11T23:36:14.358+0800 I STORAGE  [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating