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格式和详细例子 有 32 条回应

  1. That makes it complex won’t help you with improving the quality.
    You can find a list among the ggood free and paid directories in my small site.
    Use your most satisfied clients to get more more business too your doorstep.

    回复
  2. I’d like to thank you for the efforts you’ve put in penning
    this site. I really hope to check ouut the same high-grade contennt from you in the future as well.
    In truth, your creative writiong abilities has motivated me to get my
    own, personal webite now 😉

    回复
  3. Hi to every , as I am truly eager of reading this blog’s post to be updated daily. It consists of fastidious information.| а

    回复
  4. Superb blog! Do you have any suggestions for
    aspiring writers? I’m planning to start my own site soon but I’m a
    little lost on everything. Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many options out
    there that I’m totally overwhelmed .. Any recommendations?
    Thank you!

    回复
  5. Thank you, I have recently been looking for info approximately this topic
    for a while and yours is the greatest I have discovered
    so far. However, what about the bottom line? Are you certain in regards
    to the source?

    回复
  6. Hey there would you mind letting me know which web host you’re
    utilizing? I’ve loaded your blog in 3 completely different browsers and
    I must say this blog loads a lot quicker then most.

    Can you recommend a good web hosting provider at
    a honest price? Thanks a lot, I appreciate it!

    回复
  7. I got this website from my pal who told me concerning this website and
    at the moment this time I am browsing this site and reading very informative content
    here.

    回复
  8. My coder is trying to convince me to move to
    .net from PHP. I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using Movable-type on several websites for about a year and am nervous about switching to another
    platform. I have heard great things about blogengine.net.
    Is there a way I can import all my wordpress content into
    it? Any kind of help would be really appreciated!

    回复
  9. Woah! I’m really enjoying the template/theme of this site.
    It’s simple, yet effective. A lot of times it’s very difficult to
    get that “perfect balance” between user friendliness
    and visual appeal. I must say you have done a great job with this.
    Additionally, the blog loads super quick for me on Opera.
    Outstanding Blog!

    回复
  10. Wow, superb blog structure! How long have you been blogging for?
    you make running a blog glance easy. The total look of your
    web site is great, as neatly as the content material!

    回复
  11. Hi! Someone in my Facebook group shared this website with us so I came to check it out.
    I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my
    followers! Great blog and excellent design and
    style.

    回复
  12. I absolutely love your blog.. Great colors & theme.
    Did you develop this site yourself? Please reply back as I’m looking to create my own site and would like
    to learn where you got this from or what the theme is called.

    Appreciate it!

    回复
  13. you are in point of fact a good webmaster. The website loading velocity is amazing.

    It kind of feels that you’re doing any distinctive trick. Moreover, The
    contents are masterwork. you have done a excellent activity on this matter!

    回复
  14. Hello! I just wish to give you a huge thumbs up for the great information you’ve got right here
    on this post. I will be returning to your blog for
    more soon.

    回复
  15. Hello, I log on to your blog daily. Your writing style is witty, keep it up!

    回复
  16. A fascinating discussion is worth comment. I do think that you should publish more on this subject, it may not be
    a taboo subject but typically people do not discuss these subjects.
    To the next! Many thanks!!

    回复
  17. Way cool! Some extremely valid points! I appreciate
    you penning this post and also the rest of the website is also very good.

    回复
  18. 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 three e-mails with the
    same comment. Is there any way you can remove me from that service?
    Thank you!

    回复
  19. bookmarked!!, I like your blog!

    回复
  20. Quality articles is the important to attract the viewers to pay a quick visit the web page, that’s
    what this site is providing.

    回复
  21. You ought to take part in a contest for one of the
    finest sites on the net. I’m going to recommend this website!

    回复
  22. Your means of describing everything in this piece of writing is really fastidious, every one be capable of without difficulty understand it,
    Thanks a lot.

    回复
  23. You need to be a part of a contest for one of the greatest blogs
    online. I’m going to recommend this site!

    回复
  24. Nice blog here! Also your site loads up very fast!
    What host are you using? Can I get your affiliate link to your host?
    I wish my website loaded up as fast as yours lol

    回复
  25. Hello, i think that i saw you visited my website so i came to
    “return the favor”.I am trying to find things to enhance my website!I suppose its ok
    to use some of your ideas!!

    回复
  26. I don’t even know how I finished up right here,
    but I assumed this publish was great. I do
    not understand who you’re but certainly you’re going to a famous blogger in the event you
    aren’t already. Cheers!

    回复
  27. 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.

    回复
  28. Hi there, its nice piece of writing about media print,
    we all be aware of media is a enormous source of data.

    回复
  29. As the admin of this web site is working, no doubt very rapidly it will be famous, due to its quality contents.

    回复
  30. Highly energetic blog, I loved that bit. Will there be a part 2?

    回复
  31. These are truly great ideas in about blogging. You
    have touched some nice points here. Any way keep up wrinting.

    回复
  32. This is a topic which is close to my heart…

    Take care! Exactly where are your contact details though?

    回复

发表评论

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