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

    回复

发表评论

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