Feed Rss

【收录】linux crontab格式和详细例子

09.26.2012, Linux, by .

linux crontab格式和详细例子
基本格式 :
* * * * * command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每月1、10、22日的4 : 45重启apache。
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每周六、周日的1 : 10重启apache。
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启apache。
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每星期六的11 : 00 pm重启apache。
* */1 * * * /usr/local/etc/rc.d/lighttpd restart


* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart

名称 : crontab
使用权限 : 所有使用者
使用方式 :
crontab file [-u user]-用指定的文件替代目前的crontab。
crontab-[-u user]-用标准输入替代目前的crontab.
crontab-c dir- 指定crontab的目录。
crontab文件的格式:M H D m d cmd.

M: 分钟(0-59)。
m: 月(1-12)。
d: 一星期内的天(0~6,0为星期天)。
说明 :
crontab 是用来让使用者在固定时间或固定间隔执行程序之用,换句话说,也就是类似使用者的时程表。-u user 是指设定指定
user 的时程表,这个前提是你必须要有其权限(比如说是 root)才能够指定他人的时程表。如果不使用 -u user 的话,就是表示设
参数 :
crontab -e : 执行文字编辑器来设定时程表,内定的文字编辑器是 VI,如果你想用别的文字编辑器,则请先设定 VISUAL 环境变数
来指定使用那个文字编辑器(比如说 setenv VISUAL joe)
crontab -r : 删除目前的时程表
crontab -l : 列出目前的时程表
crontab file [-u user]-用指定的文件替代目前的crontab。
时程表的格式如下 :
f1 f2 f3 f4 f5 program
其中 f1 是表示分钟,f2 表示小时,f3 表示一个月份中的第几日,f4 表示月份,f5 表示一个星期中的第几天。program 表示要执
当 f1 为 * 时表示每分钟都要执行 program,f2 为 * 时表示每小时都要执行程序,其馀类推
当 f1 为 a-b 时表示从第 a 分钟到第 b 分钟这段时间内要执行,f2 为 a-b 时表示从第 a 到第 b 小时都要执行,其馀类推
当 f1 为 */n 时表示每 n 分钟个时间间隔执行一次,f2 为 */n 表示每 n 小时个时间间隔执行一次,其馀类推
当 f1 为 a, b, c,… 时表示第 a, b, c,… 分钟要执行,f2 为 a, b, c,… 时表示第 a, b, c…个小时要执行,其馀类推
使用者也可以将所有的设定先存放在档案 file 中,用 crontab file 的方式来设定时程表。
例子 :
#每天早上7点执行一次 /bin/ls :
0 7 * * * /bin/ls
在 12 月内, 每天的早上 6 点到 12 点中,每隔3个小时执行一次 /usr/bin/backup :
0 6-12/3 * 12 * /usr/bin/backup
周一到周五每天下午 5:00 寄一封信给 :
0 17 * * 1-5 mail -s "hi" < /tmp/maildata
每月每天的午夜 0 点 20 分, 2 点 20 分, 4 点 20 分….执行 echo "haha"
20 0-23/2 * * * echo "haha"
注意 :
后加上 > /dev/null 2>&1 即可

例子2 :
10 6 * * * date
0 */2 * * * date
0 23-7/2,8 * * * date
0 11 4 * mon-wed date
0 4 1 jan * date

【收录】linux crontab格式和详细例子 有 423 条回应

  1. Excellent site you’ve got here.. It’s hard to find quality writing like yours nowadays. I truly appreciate individuals like you! Take care!!

  2. Very nice write-up. I certainly appreciate this website. Continue the good work!| а

  3. Hey there! This is my first visit to your blog! We are a collection of volunteers and starting a new project in a community in the same niche. Your blog provided us valuable information to work on. You have done a marvellous job!|

  4. You really make it seem so easy with your presentation but I find this matter to be actually something which I think I would never understand. It seems too complicated and extremely broad for me. I am looking forward for your next post, I’ll try to get the hang of it!| а

  5. I’m very happy to find this page. I want to to thank you for ones time just for this wonderful read!! I definitely appreciated every little bit of it and i also have you bookmarked to look at new things on your site.

  6. Way cool! Some very valid points! I appreciate you writing this article and also the rest of the site is also really good.

  7. Thanks for sharing your thoughts. I really appreciate your efforts and I will be waiting for your next write ups thank you once again.| а

  8. I couldn’t resist commenting. Very well written!|

  9. I think what you published was very logical. However, think about this, what if you typed a catchier post title? I ain’t suggesting your information is not good., however what if you added a title to possibly get a person’s attention? I mean %BLOG_TITLE% is a little vanilla. You might peek at Yahoo’s home page and see how they create article headlines to get viewers interested. You might try adding a video or a related picture or two to get people interested about everything’ve got to say. In my opinion, it would bring your blog a little bit more interesting.| а

  10. Great info. Lucky me I recently found your website by chance (stumbleupon). I’ve book-marked it for later!

  11. I have to thank you for the efforts you’ve put in writing this blog. I am hoping to check out the same high-grade content from you later on as well. In fact, your creative writing abilities has encouraged me to get my own, personal site now 😉

  12. Hello there! Do you know if they make any plugins to help with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success. If you know of any please share. Appreciate it!| а

  13. I used to be recommended this website by way of my cousin. I am now not certain whether or not this publish is written via him as nobody else recognise such designated approximately my problem. You’re incredible! Thanks!| а

  14. Hi, I do think this is a great web site. I stumbledupon it 😉 I may return once again since I bookmarked it. Money and freedom is the greatest way to change, may you be rich and continue to help others.

  15. Wow that was unusual. I just wrote an really long comment but after I clicked submit my comment didn’t show up. Grrrr… well I’m not writing all that over again. Anyhow, just wanted to say fantastic blog!| а

  16. I think this is one of the most important info for me. And i am glad reading your article. But wanna remark on few general things, The site style is ideal, the articles is really great : D. Good job, cheers| а

  17. It’s very simple to find out any topic on web as compared to textbooks, as I found this article at this site.| а

  18. There is definately a great deal to learn about this issue. I really like all of the points you made.

  19. I enjoy what you guys are usually up too. This sort of clever work and coverage! Keep up the terrific works guys I’ve included you guys to my blogroll.|

  20. If you would like to obtain a great deal from this paragraph then you have to apply these strategies to your won website.|

  21. Can I simply just say what a relief to find somebody that truly understands what they are discussing on the internet. You definitely realize how to bring a problem to light and make it important. More and more people really need to look at this and understand this side of your story. I can’t believe you aren’t more popular since you surely have the gift.

  22. Unquestionably believe that which you said. Your favorite justification seemed
    to be on the net the simplest thing to be aware of.
    I say to you, I definitely get annoyed while people consider worries that they just don’t know about.
    You managed to hit the nail upon the top as well as defined out
    the whole thing without having side-effects , people can take a signal.
    Will probably be back to get more. Thanks

  23. Hey there! This is kind of off topic but I need some advice from an established blog.
    Is it very hard to set up your own blog? I’m not very techincal but I can figure things out pretty quick.
    I’m thinking about setting up my own but I’m not sure where to
    begin. Do you have any points or suggestions? Many thanks



电子邮件地址不会被公开。 必填项已用*标注