MacOS:Launchd&LaunchDaemon&LaunchAgent&.plist文件编写

launchd官方文档>>
详解文章1>>
详解文章2>>

什么是launchd

来自于官方文档:”Wikipedia defines launchd as "a unified, open-source service management framework for starting, stopping and managing daemons, applications, processes, and scripts. Written and designed by Dave Zarzycki at Apple, it was introduced with Mac OS X Tiger and is licensed under the Apache License."

可以理解为,launchd是一套统一的开源服务管理框架,它用于启动、停止以及管理后台程序、应用程序、进程和脚本。launchd是macOS第一个启动的进程,该进程的PID为1,整个系统的其他进程都是它创建的。当launchd启动后,它会扫描/System/Library/LaunchDaemons/Library/LaunchDaemons中的plist文件并加载他们;当输入密码登录系统后,launchd会扫描/System/Library/LaunchdAgents/Library/LaunchAgents~/Library/LaunchAgents这三个目录中的plist文件并加载它们。每个plist文件都是一个任务,加载不代表立即运行,只有设置了RunAtLoadtruekeepAlivetrue时,才会加载并同时启动这些任务。

Daemons和Agents

守护进程,英⽂叫Daemon,守护进程其实就是在后台运⾏的程序,它没有界⾯,你看不到它,⼀般使⽤命令来对它进程管理控制,守护进程常被设置为开机⾃动启动(当然也可以开机后⼿动⽤命令启动),很多软件的“服务器端”⼀般都是以守护进程的⽅式运⾏,⽐如数据库 、内存缓存 、Web服务器 等等都是以守护进程⽅式运⾏的,它们通过“接⼝”对外提供服务(如Unix socket / tcp ⽅式等等)。
DaemonsAgents都是launchd所管理的后台程序,它们的区别是Agent是属于当前登录⽤户(就是你开机后输⼊密码时的那个⽤户名),它们是以当前登录的⽤户权限启动的,⽽Daemon则属于root⽤户,但由于有root⽤户权限,所以它可以指定以什么⽤户运⾏,也可以不指定(不指定就是以root⽤户运⾏)。

launchd如何管理后台进程

launchd是通过以“.plist”后缀结尾的xml⽂件来定义⼀个程序的开机⾃启动的,我们⼀般称它为plist⽂件。

编写一个简单的.plist配置文件,启动一个Agent进程

1.使用XCode编写一个需要运行的程序launchd_test

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSProcessInfo *proc = [NSProcessInfo processInfo];
        NSArray *args = [proc arguments];
        // insert code here...
        NSLog(@"%@",args[1]);
        NSLog(@"Hello, World!");
    }
    return 0;
}

ctrl +B编译成一个可执行程序launchd_test

2.编写.plist配置文件

launchd会读取.plist文件,进而决定如何启动该进程

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3 <plist version="1.0">
  4     <dict>
  5         <key>Label</key>
  6         <string>local.launchd_test.app</string>
  7         <key>Program</key>
  8         <string>/Users/mac/Documents/launchd_test</string>
  9         <key>ProgramArguments</key>
 10         <array>
 11                 <string>/Users/mac/Documents/launchd_test</string>
 12                 <string>happy</string>
 13         </array>
 14         <key>RunAtLoad</key>
 15         <true/>
 16     </dict>
 17 </plist>

Label为需要启动的进程设置一个标签,这里起名为local.launchd_test.app
Program设置为需要启动的可执行程序的路径
ProgramArguments设置为程序运行需要的参数,第一个参数即为main函数中的argv[0]
RunAtLoad设置为是否在加载时启动,设置为true则在launchd加载时就启动,false则需要手动启动
将此文件命名为local.launchd_test.app.plist 对于用户代理进程,我们将.plist文件放到~/Library/LaunchAgents.文件中,对于守护进程,我们将其放到/Library/LaunchDaemons文件中

3.加载、卸载、启动、停止、查看launchd列表

列出所有由launchd管理的进程

launchctl list 

查看某个具体的进程

host:~ user$ launchctl list | grep com.example.app
-	2	com.example.app

1.首先,创建完.plist文件后,需要加载该文件,

launchctl load ~/Library/LaunchAgents/local.launchd_test.app.plist

正常不会输出任何东西,如果输出

Load failed: 5: Input/output error
Try running `launchctl bootstrap` as root for richer errors.

可以尝试

launchctl unload ~/Library/LaunchAgents/local.launchd_test.app.plist
launchctl load ~/Library/LaunchAgents/local.launchd_test.app.plist

2.launchd启动代理进程

launchctl start local.launchd_test.app

此时,可以通过launchctl list查看到launchd_test的进程号

launchctl list | grep local.launchd_test.app

卸载、关闭命令

launchctl unload ~/Library/LaunchAgents/local.launchd_test.app.plist
launchctl stop local.launchd_test.app

通过launchd启动进程后,bash上看不到输出,如果需要查看输出,需要将NSLog输出重定向到指定文件夹===》

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Michael.Scofield

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值