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

  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
    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

  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?



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