MacOS添加自启动脚本

MacOS下添加自启动脚本有很多方法, 在一篇知乎文章中了解到Launchd替代了过去的init, rc, init.d, rc.d, SystemStarter, inted/xinetd, watchdogd等, 建议用Launchd.
当然还有别的Automator, Apple Script等方式(底层未研究), 感兴趣的自己搜索, 我选择了直接Launchd, 结合so上的这篇文章:

  1. 编写自己的脚本, 添加可执行权限chmod a+x myscript.sh
  2. 编写Launchd配置文件(.plist文件)
  3. 结合上述两篇文章, 确定在系统启动还是用户启动时运行脚本, 我选择的是用户目录(~/Library/LaunchAgents/)
  4. load这个配置: launchctl load -w ~/Library/LaunchAgents/com.service.name.plist
  5. 登入登出测试, 或: launchctl start com.service.name

注:

  1. 可执行脚本里的路径有空格需要转义
  2. 但plist文件里<string>标签里的目录如果有空格, 不需要转义
  3. load-w参数参见这篇文章
  4. 如果出错, 运行Console应用查看日志, 或参考这篇文章, 定向日志输出文件
    即在.plist文件里添加:
<key>StandardOutPath</key>
<string>/var/log/mylog.log</string>

附: .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>com.service.name</string>
        <key>ProgramArguments</key>
        <array>
            <string>/path/to/my/script.sh</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
    </dict>
</plist>

如果执行的脚本就一句话, 你可能希望直接在.plist文件里运行, 而不是额外再多生成一个脚本吧? (source)

<key>ProgramArguments</key>
<array>
        <string>/bin/bash</string>
        <string>-c</string>
        <string>ls -1 | grep *.txt | echo &gt; allTextFiles</string>
</array>

继续, 如果还想以root来执行脚本, 综合起来, 我的实现如下:

cp com.run.udp2raw.plist /Library/LaunchDaemons
cd /Library/LaunchDaemons
sudo launchctl load -w com.run.udp2raw.plist
sudo launchctl start com.run.udp2raw

其中udp2raw对应的命令是需要root权限的, 实测通过. 我选择的是/Library/LaunchDaemons/

注: 唯一要注意的地方, 就是最后两行, loadstart命令都需要加sudo. 没有加的时候没有报错, 但是没有运行成功.

附: folders and usage

|------------------|-----------------------------------|---------------------------------------------------|
| User Agents      | ~/Library/LaunchAgents            | Currently logged in user
|------------------|-----------------------------------|---------------------------------------------------|
| Global Agents    | /Library/LaunchAgents             | Currently logged in user
|------------------|-----------------------------------|---------------------------------------------------|
| Global Daemons   | /Library/LaunchDaemons            | root or the user specified with the key UserName
|------------------|-----------------------------------|---------------------------------------------------|
| System Agents    | /System/Library/LaunchAgents      | Currently logged in user
|------------------|-----------------------------------|---------------------------------------------------|
| System Daemons   | /System/Library/LaunchDaemons     | root or the user specified with the key UserName
|------------------|-----------------------------------|---------------------------------------------------|
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Login Items Mac OSX的当前用户成功登录后启动的程序,该类别的启动项配置文件存放在~/Librar...
    贰爷阅读 10,813评论 0 0
  • 0.准备知识 Launchd是Darwin核心启动后所调用的第一个也是唯一一个进程,剩下所有的系统服务和用户进程都...
    ljjkerwin阅读 8,190评论 0 0
  • 最近准备着手搭建一些方便团队开发工作顺利进行的工具平台,首先就从Jenkins打包开始吧。其实关于ios的自动化打...
    路_远方阅读 4,704评论 1 1
  • 创建定时任务主要就是为了每天固定运行一下脚本之类的。比如cocoapods仓库每天总是有新的第三方库提交,那么po...
    齐滇大圣阅读 11,695评论 1 9
  • 希望是什么?未来自己的某种状态。 没有希望,会让你感觉自己活着就像是茶几,摆满了悲剧。 希望是随随便便来的?还是努...
    魏_7dbf阅读 548评论 0 0

友情链接更多精彩内容