linux下面定时执行一个脚本只需要crontab或者at以下就好了.mac上是是什么?怎么运行一个任务?
- 首先,你要写一个任务.
其次,让这个任务定时执行.
简单的写一个任务get_time.sh
#!/bin/bash
date >> /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/LaunchDaemons
launchctl load -w /Library/LaunchDaemons/com.apple.getdates.plist
launchctl list | grep getdates
验证是否生效
tail -f /Users/apple/dates.txt
由于这个脚本仅仅是为了演示,所以,记得把他给卸载,删除
launchctl unload -w /Library/LaunchDaemons/com.apple.getdates.plist
rm /Library/LaunchDaemons/com.apple.getdates.plist