LSX-blog

Feed Rss

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

09.26.2012, Linux, by .

linux crontab格式和详细例子
基本格式 :
* * * * * command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
crontab文件的一些例子:
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每晚的21:30重启apache。
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
每一小时重启apache

 

* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
晚上11点到早上7点之间,每隔一小时重启apache
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
每月的4号与每周一到周三的11点重启apache
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
一月一号的4点重启apache

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

M: 分钟(0-59)。
H:小时(0-23)。
D:天(1-31)。
m: 月(1-12)。
d: 一星期内的天(0~6,0为星期天)。
cmd要运行的程序,程序被送入sh执行,这个shell只有USER,HOME,SHELL这三个环境变量
说明 :
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 寄一封信给 alex@domain.name :
0 17 * * 1-5 mail -s "hi" alex@domain.name < /tmp/maildata
每月每天的午夜 0 点 20 分, 2 点 20 分, 4 点 20 分….执行 echo "haha"
20 0-23/2 * * * echo "haha"
注意 :
当程序在你所指定的时间执行后,系统会寄一封信给你,显示该程序执行的内容,若是你不希望收到这样的信,请在每一行空一格之
后加上 > /dev/null 2>&1 即可

例子2 :
#每天早上6点10分
10 6 * * * date
#每两个小时
0 */2 * * * date
#晚上11点到早上8点之间每两个小时,早上8点
0 23-7/2,8 * * * date
#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 11 4 * mon-wed date
#1月份日早上4点
0 4 1 jan * date

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

  1. Thanks for sharing your thoughts on %meta_keyword%. Regards| а

    回复
  2. Thanks for your marvelous posting! I truly enjoyed reading it, you could be a great author. I will ensure that I bookmark your blog and will often come back in the foreseeable future. I want to encourage you to continue your great posts, have a nice afternoon!| а

    回复
  3. Why people still use to read news papers when in this technological globe all is existing on web?| а

    回复
  4. Every weekend i used to pay a quick visit this web page, as i wish for enjoyment, for the reason that this this web page conations really nice funny material too.| а

    回复
  5. When I initially left a comment I appear to have clicked on the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I recieve 4 emails with the exact same comment. Is there a means you can remove me from that service? Thanks a lot!| а

    回复
  6. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several e-mails with the same comment. Is there any way you can remove people from that service? Thank you!| а

    回复
  7. Very descriptive blog, I loved that bit. Will there be a part 2?| а

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

    回复
  9. Nice replies in return of this query with solid arguments and describing the whole thing regarding that.| а

    回复
  10. Pretty nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I will be subscribing to your rss feed and I hope you write again very soon!| а

    回复
  11. I love your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Plz respond as I’m looking to construct my own blog and would like to find out where u got this from. cheers| а

    回复
  12. Heya! I just wanted to ask if you ever have any trouble with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no backup. Do you have any methods to protect against hackers?| а

    回复
  13. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.| а

    回复
  14. I have read so many posts concerning the blogger lovers except this article is genuinely a good piece of writing, keep it up.| а

    回复
  15. Hello! I’m at work surfing around your blog from my new iphone 3gs! Just wanted to say I love reading through your blog and look forward to all your posts! Carry on the outstanding work!| а

    回复
  16. Hello There. I found your blog using msn. This is an extremely well written article. I will make sure to bookmark it and return to read more of your useful information. Thanks for the post. I will certainly comeback.| а

    回复
  17. You ought to take part in a contest for one of the highest quality sites on the web. I will recommend this web site!| а

    回复
  18. Please let me know if you’re looking for a article writer for your weblog. You have some really great articles and I believe I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some content for your blog in exchange for a link back to mine. Please send me an email if interested. Many thanks!| а

    回复
  19. excellent put up, very informative. I’m wondering why the opposite experts of this sector don’t understand this. You must continue your writing. I’m sure, you’ve a great readers’ base already!| а

    回复
  20. Have you ever thought about adding a little bit more than just your articles? I mean, what you say is valuable and everything. However imagine if you added some great visuals or videos to give your posts more, “pop”! Your content is excellent but with pics and video clips, this site could definitely be one of the most beneficial in its niche. Good blog!| а

    回复
  21. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what youre talking about, why waste your intelligence on just posting videos to your site when you could be giving us something informative to read?| а

    回复
  22. I know this if off topic but I’m looking into starting my own weblog and was curious what all is needed to get setup? I’m assuming having a blog like yours would cost a pretty penny? I’m not very internet savvy so I’m not 100% positive. Any tips or advice would be greatly appreciated. Cheers| а

    回复
  23. I believe that is one of the so much vital information for me. And i’m happy reading your article. But should statement on some common issues, The website style is ideal, the articles is truly great : D. Excellent task, cheers| а

    回复
  24. Hi, I do think this is a great web site. I stumbledupon it 😉 I may come back once again since I bookmarked it. Money and freedom is the best way to change, may you be rich and continue to help others.| а

    回复
  25. What’s Taking place i am new to this, I stumbled upon this I have found It absolutely useful and it has helped me out loads. I hope to give a contribution & assist different users like its aided me. Good job.| а

    回复
  26. Thanks for another wonderful article. Where else may anybody get that type of information in such an ideal way of writing? I have a presentation subsequent week, and I am at the search for such information.| а

    回复
  27. You are so awesome! I do not believe I’ve truly read something like this before. So nice to find another person with a few genuine thoughts on this topic. Seriously.. thank you for starting this up. This site is something that’s needed on the internet, someone with some originality!| а

    回复
  28. May I just say what a relief to discover someone that truly understands what they’re talking about over the internet. You definitely realize how to bring an issue to light and make it important. More people really need to read this and understand this side of your story. I can’t believe you are not more popular given that you certainly possess the gift.| а

    回复
  29. You can definitely see your enthusiasm within the work you write. The world hopes for more passionate writers like you who aren’t afraid to say how they believe. All the time go after your heart.| а

    回复
  30. Woah! I’m really enjoying the template/theme of this blog. It’s simple, yet effective. A lot of times it’s hard to get that “perfect balance” between superb usability and appearance. I must say you have done a fantastic job with this. In addition, the blog loads super fast for me on Chrome. Outstanding Blog!| а

    回复
  31. Definitely believe that which you said. Your favorite justification appeared to be on the web the easiest thing to be aware of. I say to you, I certainly get irked while people think about worries that they plainly do not know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side effect , people could take a signal. Will probably be back to get more. Thanks| а

    回复
  32. I think this is among the most important info for me. And i’m glad reading your article. But wanna remark on some general things, The site style is ideal, the articles is really excellent : D. Good job, cheers| а

    回复
  33. Hi, i think that i saw you visited my site so i came to “return the favor”.I’m attempting to find things to improve my website!I suppose its ok to use some of your ideas!!| а

    回复
  34. If you desire to obtain a good deal from this article then you have to apply these strategies to your won blog.| а

    回复

发表评论

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