linux添加计划任务

crond 是linux用来定期执行程序的命令。当安装完成操作系统之后,默认便会启动此任务调度命令。crond命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作。可以用以下的方法启动、关闭这个服务:

[root@host ~]# /sbin/service crond start       启动
[root@host ~]# /sbin/service crond stop       关闭
Stopping crond:                                            [  OK  ]
[root@host ~]# /sbin/service crond start      启动
Starting crond:                                            [  OK  ]
[root@host ~]# /sbin/service crond restart   重新启动
Stopping crond:                                            [  OK  ]
Starting crond:                                            [  OK  ]

[root@host ~]# /sbin/service crond reload   重新载入配置
Reloading crond:                                           [  OK  ]

Linux下的任务调度分为两类,系统任务调度和用户任务调度。

系统任务调度:系统周期性所要执行的工作,比如写缓存数据到硬盘、日志清理等。

在/etc目录下有一个crontab文件,这个就是系统任务调度的配置文件。

[root@host ~]# cat /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

# For details see man 4 crontabs

# For details see man 4 crontabs

# Example of job definition:

# .---------------- minute (0 - 59)

# |  .------------- hour (0 - 23)

# |  |  .---------- day of month (1 - 31)

# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...

# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

# |  |  |  |  |

# *  *  *  *  * user-name command to be executed

第一行SHELL变量指定了系统要使用哪个shell,这里是bash

第二行PATH变量指定了系统执行 命令的路径

第三行MAILTO变量指定了crond的任务执行信息将通过电子邮件发送给root用户,如果MAILTO变量的值为空,则表示不发送任务 执行信息给用户

第四行的HOME变量指定了在执行命令或者脚本时使用的主目录。

用户任务调度:用户定期要执行的工作,比如用户数据备份等。用户可以使用 crontab 工具来定制自己的计划任务。

所有用户定义的crontab 文件都被保存在 /var/spool/cron目录中。其文件名与用户名一致。

crontab命令选项:

cron服务提供crontab命令来设定cron服务的,以下是这个命令的一些参数与说明:

crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数

crontab -l //列出某个用户cron服务的详细内容

crontab -r //删除没个用户的cron服务

crontab -e //编辑某个用户的cron服务

比如说root查看自己的cron设置:crontab -u root -l

再例如,root想删除fred的cron设置:crontab -u fred -r

在编辑cron服务时,编辑的内容有一些格式和约定,输入:crontab -u root -e

cron文件语法

  分     小时      日       月       星期      命令

  0-59   0-23   1-31   1-12     0-6     command 

特殊符号的含义:

"*"代表取值范围内的数字

"/"代表"每"

"-"代表从某个数字到某个数字

","分开几个离散的数字

举例说明

5       *       *       *      *       ls              //指定每小时的第5分钟执行一次ls命令

30     7       8       *      *      ls             //每月8号的7:30分执行ls命令

30     6       *       *      0      ls            //每周日的6:30分执行ls

30     3   10,20     *      *      ls           //每月10,20号的3:30分执行ls

10    1-5   *        *     *      ls              //每天1到5点,每隔10分钟执行ls

*/5       *       *       *      *    ls         //每5分钟执行一次ls

30     6    */10      *     *      ls         //每个月每隔10天6:30分执行一次ls

数据备份计划任务

创建shell脚本如下:

[root@host ~]# cat /root/shellfile/dbbak.sh   
mysqldump -u root -proot test>/root/dbbak/test_`date +%Y%m%d%H%M`.sql

修改文件的可执行权限

[root@host ~]# chmod a+x /root/shellfile/dbbak.sh

新增调度任务

[root@host shellfile]# crontab -e

no crontab for root - using an empty one

*/5 * * * * /root/shellfile/dbbak.sh

查看执行情况

[root@host dbbak]# ls -al |grep 201805   
-rw-r--r--  1 root root   8154 May 29 16:55 test_201805291655.sql

[root@host dbbak]# ls -al |grep 201805
-rw-r--r--  1 root root   8154 May 29 16:55 test_201805291655.sql
-rw-r--r--  1 root root   8154 May 29 17:00 test_201805291700.sql
-rw-r--r--  1 root root   8154 May 29 17:05 test_201805291705.sql

原文地址:https://www.cnblogs.com/playforever/p/9105398.html