duanmu0834 2019-05-31 16:00
浏览 141
已采纳

如何设置已经运行的二进制文件以在启动时运行

I have a Go program that is supposed to run as a daemon with minimal front-end for the user (just an icon in the system tray.)

The fact that it's written in Go doesn't really matter much, the main issue I have is getting a binary (that is already running) to run at startup on a Mac.

I can set the binary to run at startup like so:

cp daemon.plist ~/Library/LaunchAgents/daemon.plist

(cp the plist into the LaunchAgents directory)

launchctl load -w ~/Library/LaunchAgents/daemon.plist

(then load the plist with launchctl.)

And that works fine. The only issue is that launchctl load it starts another instance of the binary that is already running. I want to be able to enable (and disable) running the binary at startup for the binary that is running, without starting another instance of it.

And here's the .plist file if you want to see it:

<?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>daemon</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/Daemon.app/Contents/MacOS/daemon</string>
    </array>
    <key>ProcessType</key>
    <string>Interactive</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
</dict>
</plist>
  • 写回答

1条回答 默认 最新

  • dop82210 2019-05-31 17:40
    关注

    I figured it out.

    So first, the binary is running. It starts out as not enabled to run at startup. To initialize, you cp the plist to the ~/Library/LaunchAgents/ directory:

    cp daemon.plist ~/Library/LaunchAgents/daemon.plist
    

    And the plist looks like this:

    <?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>daemon</string>
        <key>ProgramArguments</key>
        <array>
            <string>/Applications/Daemon.app/Contents/MacOS/daemon</string>
        </array>
        <key>ProcessType</key>
        <string>Interactive</string>
        <key>RunAtLoad</key>
        <false/>
        <key>KeepAlive</key>
        <false/>
    </dict>
    </plist>
    

    NOTE: The RunAtLoad key is set to false.

    Then we load it into launchctl:

    launchctl load -w ~/Library/LaunchAgents/daemon.plist
    

    It shouldn't load since RunAtLoad is set to false.

    Then, we can enable and disable running at startup like so:

    To enable:

    • Set RunAtLoad to true
    • cp the plist over

    To disable:

    • Set RunAtLoad to false
    • cp the plist over

    Kind of hacky, but it works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则