大数据,机器学习,人工智能大数据Python

crontab定期执行命令以及案例详解

2020-02-24  本文已影响0人  叫我老村长
 _
| |__ _ _ __ _
| '_ \| | | |/ _` |
| |_) | |_| | (_| |
|_.__/ \__,_|\__, |      每天准时造bug
             |___/ 

一,什么是crond,crontab

linux下面定期分为二部分,一部分是后台程序crond,一部分是crontab往crond输入指令的接口。

为什么要定期执行,因为有些事情我们是要定时执行的,这样可以节省不少人力,物力。例如:每个星期都要给注册的用户发送邮件,如果能定时执行的话,就不要有个人去做了,现在sns比较流行,里面有什么好友新鲜事,你做的事情,不是立马你的好友就能看到,也许要过一小时,或者几个小时才能看到,如果要人工去操作的话,就比较烦了。

二,crond启动

crond不同的系统下面启动不同,以archlinux为例

1,crond启动

[root@BlackGhost zhangy]# crond

2,crond开机启动

a,利用rc.conf

DAEMONS=
(syslog-ng alsa hal net-profiles httpd !
slim @mysqld !network !netfs **crond**
)

b,利用rc.local

在文件的最后加上sudo crond

三,crontab命令详解

  1. [root@BlackGhost etc]# crontab -h
  2. crontab V3.2
  3. crontab file <opts> replace crontab from file //修改文件存放位置
  4. crontab - <opts> replace crontab from stdin
  5. crontab -u user specify user //指定用户
  6. crontab -l [user] list crontab for user // 查看命令列表
  7. crontab -e [user] edit crontab for user //编辑列表
  8. crontab -d [user] delete crontab for user //删除列表
  9. crontab -c dir specify crontab directory //指定crontab 目录
[root@BlackGhost etc]# crontab -hcrontab V3.2crontab file <opts>  replace crontab from file   //修改文件存放位置
crontab -    <opts>  replace crontab from stdincrontab -u user      specify user //指定用户
crontab -l [user]    list crontab for user // 查看命令列表
crontab -e [user]    edit 
crontab for user //编辑列表
crontab -d [user]    
delete crontab for user //删除列表
crontab -c dir       specify crontab directory //指定crontab 目录

四,crontab命令详解

1,crontab file 指定命令存放

archlinux下面默认crontab命令存放在是在/var/spool/cron/下面,以用户名来命名文件的

[root@BlackGhost etc]# ls /var/spool/cron
root  zhangy

如果我想换个位置怎么办呢,看下面

1.  [root@BlackGhost etc]# cp /**var**/spool/cron/root /tmp/root 
2.  [root@BlackGhost etc]# crontab /tmp/root    //修改文件存放位置 
3.  [root@BlackGhost etc]# crontab -l 
5. #DO NOT EDIT THIS FILE MANUALLY!! USE crontab -e INSTEAD. 
8.  # <minute> <hour> <day> <month> <dow> <command> 
10.  01 * * * *  /usr/sbin/run-cron /etc/cron.hourly 
11.  02 00 * * * /usr/sbin/run-cron /etc/cron.daily 
12.  22 00 * * 0 /usr/sbin/run-cron /etc/cron.weekly 
13.  42 00 1 * * /usr/sbin/run-cron /etc/cron.monthly 
14.  #30 00 * * * /sbin/shutdown -h now 
15.  00 18 * * * /home/zhangy/database_bak.sh 
[root@BlackGhost etc]# cp /var/spool/cron/root /tmp/root[root@BlackGhost etc]# crontab /tmp/root    //修改文件存放位置
[root@BlackGhost etc]# crontab -l
## DO NOT EDIT THIS FILE MANUALLY!! USE crontab -e INSTEAD.
# # <minute> <hour> <day> <month> <dow> <command>
 01 * * * *  /usr/sbin/run-cron /etc/cron.hourly
02 00 * * * /usr/sbin/run-cron /etc/cron.daily
22 00 * * 0 /usr/sbin/run-cron /etc/cron.weekly
42 00 1 * * /usr/sbin/run-cron /etc/cron.monthly
#30 00 * * * /sbin/shutdown -h now00 18 * * * /home/zhangy/database_bak.sh

2,crontab -u,crontab -l,crontab -e,crontab -d

  1. [root@BlackGhost etc]# crontab -l -u zhangy //没有任务
  2. [root@BlackGhost etc]# crontab -e -u zhangy //添加任务
  3. [root@BlackGhost etc]# crontab -l -u zhangy //查看任务
  4. 00 18 * * * /home/zhangy/database_bak.sh
  5. [root@BlackGhost etc]# crontab -d -u zhangy //删除任务用户
  6. [root@BlackGhost etc]# crontab -l -u zhangy //用户已删除
  7. no crontab for zhangy
  8. [root@BlackGhost etc]# crontab -e -u zhangy //添加任务
  9. [root@BlackGhost etc]# crontab -l -u zhangy //查看
  10. 00 18 * * * /home/zhangy/database_bak.sh
[root@BlackGhost etc]# crontab -l -u zhangy  //没有任务[root@BlackGhost etc]# crontab -e -u zhangy  //添加任务[root@BlackGhost etc]# crontab -l -u zhangy  //查看任务
00 18 * * * /home/zhangy/database_bak.sh
[root@BlackGhost etc]# crontab -d -u zhangy  //删除任务用户[root@BlackGhost etc]# crontab -l -u zhangy  //用户已删除no crontab for zhangy
[root@BlackGhost etc]# crontab -e -u zhangy  //添加任务[root@BlackGhost etc]# crontab -l -u zhangy  //查看00 18 * * * /home/zhangy/database_bak.sh

3,crontab书写規则

<minute> <hour> <day> <month> <dow> <command>
第1列 第2列 3 4 5 6

第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重启lighttpd 。

45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每月1、10、22日的4 : 45重启lighttpd 。

10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每周六、周日的1 : 10重启lighttpd 。

0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启lighttpd 。

0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每星期六的11 : 00 pm重启lighttpd 。

0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
每月的4号与每周一到周三的11点重启lighttpd

0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
一月一号的4点重启lighttpd

上一篇 下一篇

猜你喜欢

热点阅读