mac 定时任务

来源:互联网 发布:在淘宝买手机是真品吗 编辑:程序博客网 时间:2024/05/16 09:30
linux下面定时执行一个脚本只需要crontab或者at以下就好了.mac上是是什么?怎么运行一个任务?
  • 首先,你要写一个任务.
  • 其次,让这个任务定时执行.

    简单的写一个任务get_time.sh

#!/bin/bashdate >> /Users/twocucao/Downloads/dates.txt

创建一个特殊的xml文件叫做com.apple.getdates.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.apple.getdates</string>    <key>ProgramArguments</key>    <array>        <string>/Users/apple/get_time.sh</string>    </array>    <key>StartInterval</key>    <integer>10</integer></dict></plist>

拷贝到LaunchDaemons,加载定时工作,然后检查是否加载成功

sudo cp com.apple.getdates.plist /Library/LaunchDaemonslaunchctl load -w /Library/LaunchDaemons/com.apple.getdates.plistlaunchctl list | grep getdates

验证是否生效

tail -f /Users/apple/dates.txt

由于这个脚本仅仅是为了演示,所以,记得把他给卸载,删除

launchctl unload -w /Library/LaunchDaemons/com.apple.getdates.plistrm /Library/LaunchDaemons/com.apple.getdates.plist
0 0
原创粉丝点击