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.

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

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误