1. 什么是cron
cron是一个系统的守护进程。你可以指定它在你想要的时间干活。让它干活的方法就是:2. 使用cron
- 写好一个脚本(bash/python/perl),里面包括你要干的活
- 在你的cron配置文件中告诉cron在哪个时间去执行这个脚本。
创建一个脚本文件foo.sh,让脚本把当前时间存到文本里:
创建你的crontab文件:echo `date` >> ~/foo.txt
查看你的crontab文件:crontab -e输入:
* * * * * /path/to/foo.sh
表示每分钟执行foo.sh一次。
删除你的crontab文件:crontab -l
crontab -r
然后可以把foo.sh改成你要的任何脚本。用"crontab -e"把时间改成你想要的时间。关于crontab的脚本格式,查看[1][2]或者Appendix。
References:
1. Newbie: Intro to cron, http://www.unixgeeks.org/security/newbie/unix/cron-1.html
2. $ man crontab
Appendix
e.g.
01 * * * * echo "This command is run at one min past every hour"
17 8 * * * echo "This command is run daily at 8:17 am"
17 20 * * * echo "This command is run daily at 8:17 pm"
00 4 * * 0 echo "This command is run at 4 am every Sunday"
* 4 * * Sun echo "So is this"
42 4 1 * * echo "This command is run 4:42 am every 1st of the month"
01 * 19 07 * echo "This command is run hourly on the 19th of July"