MAC下定时任务设置与网络事件监听并执行相应脚本

最近有个想法,就是想在MAC网络切换的时候,自动根据网络切换来判断是办公环境还是家庭环境,从而执行对应的动作

做技术的都知道,我们在linux下一般用crontab实现定时任务。

在MAC下,我们一般用launchd实现定时任务,以及其他事件触发的任务。

简单介绍下launchd,launchd存在于MAC OX的系统进程中,用户不能直接对该进程进行操作,只能通过launchctl对其进行管理。

launchctl是一个统一的服务管理框架,可以启动、停止和管理守护进程、应用程序、进程和脚本等。launchctl是通过配置文件来指定执行周期和任务的。配置文件一般是plist类型的文件。

属性列表(Property List)文件是一种用来存储序列化后的对象的文件。属性列表文件的文件扩展名为.plist,因此通常被称为plist文件。Plist文件通常用于存储用户设置,也可以用于存储捆绑的信息,plist中主要的字段和它的含义如下:

- Label用来在launchd中的一个唯一标识,如同每一个程序都有一个identifies一样。

- UserName指定运行启动项的用户,只有当Launchd 作为 root 用户运行时,此项才适用。

- GroupName指定运行启动项的组,只有当Launchd 作为 root 用户运行时,此项才适用。

- KeepAlive这个key值是用来控制可执行文件是持续运行,还是满足具体条件之后再启动。默认值为false,也就是满足具体条件之后才启动。当设置值为ture时,表明无条件的开启可执行文件,并使之保持在整个系统运行周期内。

- RunAtLoad标识launchd在加载完该项服务之后立即启动路径指定的可执行文件。默认值为false。

- Program这个值用来指定进程的可执行文件路径。

- ProgramArguments这个值用来指定可执行文件和运行的参数。

全部字段含义如下

KeyDescriptionRequired
LabelThe name of the jobyes
ProgramArgumentsStrings to pass to the program when it is executedyes
UserNameThe job will be run as the given user, who may not necessarily be the one who submitted it to launchd.no
inetdCompatibilityIndicates that the daemon expects to be run as if it were launched by?inetdno
ProgramThe path to your executable. This key can save the ProgramArguments key for flags and arguments.no
onDemandA?boolean?flag that defines if a job runs continuously or notno
RootDirectoryThe job will be?chrooted?into another directoryno
ServiceIPCWhether the daemon can speak IPC to launchdno
WatchPathsAllows launchd to start a job based on modifications at a file-system pathno
QueueDirectoriesSimilar to WatchPath, a queue will only watch an empty directory for new filesno
StartIntervalUsed to schedule a job that runs on a repeating schedule. Specified as the number of seconds to wait between runs.no
StartCalendarIntervalJob scheduling. The?syntax?is similar to?cron.no
HardResourceLimitsControls restriction of the resources consumed by any jobno
LowPriorityIOTells the kernel that this task is of a low priority when doing file system I/Ono
SocketsAn array can be used to specify what socket the daemon will listen on for launch on demandno

 

plist脚本一般存放在以下目录:

  • /Library/LaunchDaemons -->只要系统启动了,哪怕用户不登陆系统也会被执行

  • /Library/LaunchAgents -->当用户登陆系统后才会被执行

    更多的plist存放目录:

    ~/Library/LaunchAgents 由用户自己定义的任务项
    /Library/LaunchAgents 由管理员为用户定义的任务项
    /Library/LaunchDaemons 由管理员定义的守护进程任务项
    /System/Library/LaunchAgents 由Mac OS X为用户定义的任务项
    /System/Library/LaunchDaemons 由Mac OS X定义的守护进程任务项

    一个简单的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>onnetworkchange</string>
	<key>ProgramArguments</key>
	<array>
		<string>python</string>
		<string>%s</string>
	</array>
    <key>StandardOutPath</key>
    <string>%s</string>  
    <key>StandardErrorPath</key>  
    <string>%s</string>  
	<key>WatchPaths</key>
	<array>
		<string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
	</array>
</dict>
</plist>

launchd加载plist文件命令

launchctl load -w demo.plist

更多命令

#查看 launchctl使用手册,  man在对mac下大部分命令通用,例如 man ifconfig
$ man launchctl

# 加载任务, -w选项会将plist文件中无效的key覆盖掉,建议加上
$ launchctl load -w zrbdemo.plist

# 删除任务
$ launchctl unload -w zrbdemo.plist

# 查看任务列表
$ launchctl list 

# 开始任务
$ launchctl start zrbdemo.plist

# 结束任务
$ launchctl stop  zrbdemo.plist

更多详情参见apple官方文档

由于MAC BOOK在切换Wi-Fi网络时 /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist 文件会发生改变,利用 WatchPaths 属性即可监听该文件变化。

参照如上plist示例即可实现监听MAC网络改变事件,在触发事件时执行 ProgramArguments标签里配置的脚本文件%s

 

附带一个修改launchctl环境变量的办法(因为我遇到了launchctl使用的环境变量是系统默认环境变量导致python很多包找不到)

#将launchctl设置成当前用户的环境变量

launchctl setenv PATH $PATH #临时修改,电脑重启后会重置
sudo launchctl config user path $PATH #永久修改,电脑重启后不会重置


#不确定$PATH的值可以先

echo $PATH #打印PATH出来看看

以上纯属个人见解,有不足之处欢迎评论区喷博主~

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值