mac 下周期调度命令或脚本

crontab 是在linux服务器上部署定时任务的方法

0 5 * * * /usr/bin/python /data/www/tools/mysql_backup.py

cmd之前有5个项目要填,分别对应

分钟 小时 天 月 一周当中第几天( 0-6 ,0表示星期天)

填写方法

* 表示都满足,例如 * * * * * 表示每分钟执行一次,如果每小时执行一次,那应该这样写 
1 * * * * (每小时第一分钟执行,1可以随便改成60以内的数字)
/n 表示隔n个单位执行一次
/3 * * * * 3分钟执行一次
1 */3 * * * 表示每隔3个小时的第一分钟执行一次
1 1 */3 * * 表示每隔3天,当天的1点1分执行一次
1 12 * * 2,3,4,5,6 表示每周二到周6,每天中午12点1分执行
只需要掌握这2种时间用法,crontab就ok了

提示:

crontab 最小粒度是一分钟执行一次,要更快,得用其它办法,比如说写一个daemon程序,用while true: 来做
crontab -l 查看所有crontab 列表
cronta -e 编辑crontab
crontab条目是以文件方式存储的,可以用ls /etc/cron* 查看
标签: linuxcrontab

本文地址: http://lutaf.com/51.htm 鲁塔弗原创文章,欢迎转载,请附带原文链接

 

  

Create a script you wish to run. For this example, I am going to use the awstats.pl script to update my stats. The command to run the update is: 

/path/to/awstats.pl -update -config=myconfig

2. Create a new crontab file for your user. This is accomplished by opening the Terminal.app, found in your Application/Utilities folder. Type the following into the terminal:

crontab -e

If you do not have a crontab file, one will be created for you and opened in vi.

3. Choose when you would like the job to run. Every job is a line in your crontab file. The first 5 arguments specify the time to run the job, and the 6th argument is the command to run. 

Argument 1: Minute (0 - 59)
Argument 2: Hour (0 - 23)
Argument 3: Day of Month (1 - 31)
Argument 4: Month (1-12)
Argument 5: Day of Week (0 - 6) Sunday = 0
Argument 6: Command

So if I want to update my stats every 30 minutes, my entry in my crontab file will look like this:


*/30 * * * * /path/to/awstats.pl -update -config=myconfig

Save the file, and crontab should report: 

crontab: installing new crontab


To see a list of your crontabs, run this command: 


crontab -l

That's it! Crontab will now update the stats every half hour. It also will generate an email to your local user account to let you know it did its job. Most times your local user account's mailbox will not be where you want to be notified. To forward your email, create a file in your home directory called .forward. In the file put the email address where you would like the notification to be sent to.

 有的人抱怨说 crontab 无论正确输出还是错误输出都会往 /var/mail/*** 目录下发送邮件 append方式, 不知道别人怎么样,在mac环境下不会出现这种情况,除非 ****** cmd , cmd 有运行错误,就会输入到/var/mail/*** 里面。

 

posted @ 2015-03-23 19:33  IamThat  阅读(419)  评论(0编辑  收藏  举报