Mac launchctl 自定义服务启动
原文:https://ichochy.com/posts/20231128.html
launchd
launchd – 系统范围内的守护进程(LaunchDaemons)/代理程序(LaunchAgents)的主程序 在启动过程中,内核调用 launchd 作为第一个进程运行,并进一步引导系统的其余部分。
守护进程和服务启动图例

根据定义,守护进程是系统范围的服务,其中所有客户端都有一个实例。 代理是一种服务,以每个用户为基础运行。
守护进程不应尝试显示 UI 或直接与用户的登录会话交互。 所有涉及与用户交互地应通过代理服务完成,如:运行程序,显示 UI。
路径说明
| 路径 | 加载 | 说明 |
|---|---|---|
| /System/Library/LaunchDaemons | 系统启动 | 提供系统范围的守护进程(Apple) |
| /System/Library/LaunchAgents | 系统启动 | 提供系统范围的用户代理(Apple) |
| /Library/LaunchDaemons | 用户登录 | 提供所有用户的守护进程 |
| /Library/LaunchAgents | 用户登录 | 提供所有用户的代理进程 |
| ~/Library/LaunchAgents | 用户登录 | 提供当前用户的代理进程 |
更多具体信息查看系统帮助文档:launchd
launchd.plist
launchd.plist – 系统范围内的守护进程(LaunchDaemons)/代理程序(LaunchAgents)的配置文件
可以使用 launchctl 加载到 launchd 的列表,并根据配置文件的具体参数属性进行配置加载运行。
配置文件的命名
文件命名为 <Label>.plist。 因此,如果您的工作标签(Label)是 com.ichochy.test,您的 plist 文件应命名为:com.ichochy.test.plist
参数属性
- Label
- 作业进程的唯一标识,是必要参数
- Program
- 作业进程的执行绝对路径
Program存在时,将替代ProgramArguments的第一个参数Program不存在时,将由ProgramArguments的第一个参数替代
- ProgramArguments
- 作业进程的执行参数
Program不存在时,将由ProgramArguments的第一个参数作为作业进程的执行绝对路径
- KeepAlive
- 作业进程是否保留运行
false为默认值,停止后不再保留运行true,停止后再次启动运行- SuccessfulExit
- NetworkState
- PathState
- Crashed
- RunAtLoad
false为默认值,启动加载时不启动运行true,启动加载时启动运行
- WorkingDirectory
- 工作目录
- EnvironmentVariables
- 配置环境变量
- TimeOut
- 启动超时时间
- ExitTimeOut
- 退出超时时间
- ThrottleInterval
- 间歇时间
- StartInterval
- 间隔启动时间,单位为秒
- StartCalendarInterval
- 间隔启动时间,单位可以指定日期
- Minute
- 分钟 0-59
- Hour
- 小时 0-23
- Day
- 天 1-31
- Weekday
- 工作日 0-7
- Month
- 月 1-12
- StandardInPath
- stdin 输入信息的日志路径
- StandardOutPath
- stdout 输出信息的日志路径
- StandardErrorPath
- stderr 错误输出信息的日志路径
- ProcessType
- 进程类型,根据作业类型限制应用资源
- Background
- Standard
- Adaptive
- Interactive
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.ichochy.exampled</string>
<key>Program</key>
<string>/path/tp/exampled</string>
<key>ProgramArguments</key>
<array>
<string>exampled</string>
<string>argv1</string>
<string>argv2</string>
</array>
<key>MachServices</key>
<dict>
<key>com.ichochy.exampled</key>
<true/>
</dict>
</dict>
</plist>
“Hello World!” launchd Job
<?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>com.ichochy.hello</string>
<key>ProgramArguments</key>
<array>
<string>hello</string>
<string>world</string>
</array>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
Debugging launchd Jobs
<?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>com.ichochy.sleep</string>
<key>ProgramArguments</key>
<array>
<string>sleep</string>
<string>100</string>
</array>
<key>StandardOutPath</key>
<string>/var/log/myjob.log</string>
<key>StandardErrorPath</key>
<string>/var/log/myjob.log</string>
<key>Debug</key>
<true/>
<key>SoftResourceLimits</key>
<dict>
<key>Core</key>
<integer>9223372036854775807</integer>
</dict>
<key>HardResourceLimits</key>
<dict>
<key>Core</key>
<integer>9223372036854775807</integer>
</dict>
</dict>
</plist>
Running a Job Periodically
<?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>com.ichochy.touchsomefile</string>
<key>ProgramArguments</key>
<array>
<string>touch</string>
<string>/tmp/helloworld</string>
</array>
<key>StartInterval</key>
<integer>300</integer>
</dict>
</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>Label</key>
<string>com.ichochy.touchsomefile</string>
<key>ProgramArguments</key>
<array>
<string>touch</string>
<string>/tmp/helloworld</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>45</integer>
<key>Hour</key>
<integer>13</integer>
<key>Day</key>
<integer>7</integer>
</dict>
</dict>
</plist>
Monitoring a Directory
<?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>com.ichochy.watchhostconfig</string>
<key>ProgramArguments</key>
<array>
<string>syslog</string>
<string>-s</string>
<string>-l</string>
<string>notice</string>
<string>somebody touched /etc/hostconfig</string>
</array>
<key>WatchPaths</key>
<array>
<string>/etc/hostconfig</string>
</array>
</dict>
</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>Label</key>
<string>com.ichochy.mailpush</string>
<key>ProgramArguments</key>
<array>
<string>my_custom_mail_push_tool</string>
</array>
<key>QueueDirectories</key>
<array>
<string>/var/spool/mymailqdir</string>
</array>
</dict>
</plist>
更多具体信息查看系统帮助文档:launchd.plist
launchctl
launchctl – launchd 的管理工具
通过 launchctl 交互来管理和检查 launchd 守护进程、代理进程和 XPC 服务。
命令执行
launchctl subcommand [arguments ...]
展示加载列表
launchctl list
基本操作
# enable | disable
launchctl enable com.ichochy.example.plist #启用 plist
launchctl disable com.ichochy.example.plist #禁用 plist
# load | unload
launchctl load com.ichochy.example.plist #加载 plist
launchctl unload com.ichochy.example.plist #卸载 plist
#加载/卸载 plist,
# 参数 w 覆盖操作
# 参数 F 强制操作
launchctl load/unload [-wF] plist
launchctl remove com.ichochy.example #删除 plist
launchctl start com.ichochy.example #启动 plist
launchctl stop com.ichochy.example #停止 plist
更多具体信息查看系统帮助文档:launchctl
参考引用
launchd
launchd.plist
launchctl
https://developer.apple.com/
https://www.launchd.info/
相关文章
Mac 终端设置代理,设置一键开启和取消2023/11/25
12 个对新手最重要的 Linux 命令2022/11/08
第一次通过 SSH key 免密连接 GitHub 的完整过程2022/11/07
RIME 鼠须管输入法使用,免费开源还可以自定义词库2021/11/25
使用 GitHub Actions 自动上传搜索记录到 Algolia2021/06/12
Mac launchctl 自定义服务启动的更多相关文章
- mac下 部分服务启动,结束, 查看状态的命令
以sshd服务为例 启动sshd服务:sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist 停止sshd服务:sudo laun ...
- centos7下自定义服务启动和自动执行脚本
systemctl list-units --type=service #查看所有已启动的服务 systemctl enable httpd.service #加入开机自启动服务 systemctl ...
- Android系统在新进程中启动自定义服务过程(startService)的原理分析
在编写Android应用程序时,我们一般将一些计算型的逻辑放在一个独立的进程来处理,这样主进程仍然可以流畅地响应界面事件,提高用户体验.Android系统为我们提供了一个Service类,我们可以实现 ...
- linux开机自启动设置,自定义开机启动模版,nginx开机自启动服务
/etc/init.d 目录,我们把shell脚本放在这个目录下来作为启动脚本 都是用来放服务脚本的,当Linux启动时,会寻找这些目录中的服务脚本,并根据脚本的run level确定不同的启动级别. ...
- linux自定义开机启动服务和chkconfig使用方法
linux自定义开机启动服务和chkconfig使用方法 1. 服务概述在linux操作系统下,经常需要创建一些服务,这些服务被做成shell脚本,这些服务需要在系统启动的时候自动启动,关闭的时候自动 ...
- (转)CentOS 7 sytemctl 自定义服务开机启动
CentOS 7 sytemctl 自定义服务开机启动 原文:http://blog.csdn.net/ithomer/article/details/51766319 CentOS 7继承了RHEL ...
- (转)linux自定义开机启动服务和chkconfig使用方法
原文:https://www.cnblogs.com/jimeper/archive/2013/03/12/2955687.html linux自定义开机启动服务和chkconfig使用方法 1. 服 ...
- MongoDB做为一项windows服务启动
MongoDB做为一项windows服务启动 Windows版本安装 MongoDB的官方下载站是http://www.mongodb.org/downloads,可以去上面下载最新的对应版本,有32 ...
- Tomcat服务启动成功,但访问index.jsp出错 (jspInit)
本文引用自 --> http://zhouhaitao.iteye.com/blog/1164736 Tomcat服务启动成功,但访问index.jsp出错 环境:Tomcat6 + jdk6 ...
- Mac OS X 上启动 FTP/SFTP server,并设置 log level
木易小伟的博客| 木易小伟的博客 2013-08-13 5708 阅读 FTP Log SFTP Mac OS 系统配置 1. 启动FTP Server: 命令行下, sudo -s launch ...
随机推荐
- win10/11 禁用移动热点,无法启用
将网络重制即可
- Cannot find one or more components.
场景重现 有那么一天重启了下电脑, 打开 Microsoft SQL Server Management Studio 2016, 没有出现腻歪的用户界面, 反而出现如下异常: 错误原因 谁造呢? 有 ...
- [T.3] 团队项目:团队基础设施及 DevOps 准备
项目 内容 这个作业属于哪个课程 首页 - 2025年春季软件工程(罗杰.任健) - 北京航空航天大学 - 班级博客 - 博客园 这个作业的要求在哪里 T.3 团队项目:团队基础设施及 DevOps ...
- HTB-UnderPass
该靶机nmap扫描udp发现161端口snmp服务,利用snmpwalk扫描得到目录信息,使用dirsearch扫描得到一个yml文件,存放数据库账号密码,记录下来,此时需要登录口,使用字典扫描拼接/ ...
- Python简单数据分析
1.分析思路 以贵族价格表为例 a.使用Python连接MySQL数据库 b.从noble_right表查询贵族名称,开通价格 c.将这两组值作为XY轴绘制直方图 2.编写代码: # -*- codi ...
- DPDI Online在线kettle调度工具
1. DPDI简介 DPDI Online 是一款基于Kettle的强大在线任务调度平台,凭借其高效与灵活性,专为调度和监控Kettle客户端生成的ETL任务而设计 2. DPDI使用 2.1 DPD ...
- <HarmonyOS第一课06>构建更加丰富的页面
视频链接: https://developer.huawei.com/consumer/cn/training/course/slightMooc/C101717497640610394?ha_sou ...
- 一个日h站的Nday
0x00 前言 今天先来无事的我翻起了qq收藏夹.忽然发现了一个去年EDUSRC群里一个老表发的洞.今天就给大家发出来耍耍. 抵制黄色网站人人有责,打造绿色上网环境. 面对正规网站时候,请不要做非法测 ...
- XXL-TOOL v1.4.0 发布 | Java工具类库
Release Notes 1.[新增]JsonRpc模块:一个轻量级.跨语言远程过程调用实现,基于json.http实现(从XXL-JOB底层通讯组件提炼抽象). 2.[新增]Concurrent模 ...
- 【笔记】reko 0.10.2 反编译工具安装和使用记录|(2) user‘s guide
Reko user's guide Reko是一个二进制可执行文件的反编译器.它接受输入的一个或多个二进制可执行文件,然后反编译成高级语言.它可以在GUI shell中被交互地使用,作为一个命令行项目 ...