在启动时使用 launchd.plist 运行终端命令

问题描述 投票:0回答:1

我在 Stack Overflow 上寻找问题的答案,并找到了一个线程 (https://superuser.com/questions/229773/run-command-on-startup-login-mac-os-x)。这就是我发现

launchd.plist
的方式,然后我自己做了一个:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>js.leapmotion</string> 
    <key>OnDemand</key>
    <false/>

    <key>ProgramArguments</key>
    <array>
            <string>open</string>
            <string>/Applications/Utilities/Terminal.app</string>
    </array>
    
    <key>LaunchOnlyOnce</key>
    <true/>
</dict>
</plist>

这确实会在启动我的 Mac 后启动我的终端。但现在我希望它导航到一个目录并在其中运行命令

gulp
。 这将再次完成其余的工作,打开本地主机等等......

所以最后我只需要在特定目录中运行

gulp
即可。就这样。如果
launchd.plist
不是最好的选择,那么我愿意接受其他选择。 否则我想知道如何告诉终端应该运行哪些命令。

macos plist startup boot launchd
1个回答
0
投票

您可以使用

open
命令打开终端并使用
-a
标志运行脚本。例如,
open -a Terminal /path/to/myscript
将打开一个新的终端窗口并运行
myscript
。请注意,当
myscript
完成时,会话将终止(即没有 shell)。要改变这一点,您可以运行
bash
或任何您喜欢的 shell 作为脚本的一部分。

就您而言,我建议创建一个基本脚本来执行您希望终端执行的任何操作,例如:

#!/usr/bin/env bash

cd /path/to/your/directory
/usr/bin/gulp

保存(确保它是可执行的),然后更改你的 launchd plist 以匹配:

<key>ProgramArguments</key>
<array>
        <string>open</string>
        <string>-a</string>
        <string>Terminal</string>
        <string>/path/to/yourscript</string>
</array>
© www.soinside.com 2019 - 2024. All rights reserved.