定时执行计划任务cron——玩转Linux(四)

    1 、计划任务概说;

    2cron

        2.1 cron 使用方法; 

        2.2 cron 配置文件;

            2.2.1 编辑全局性配置文件;

            2.2.2 编辑用户自身配置文件的说明; 

    3、计划任务的应用范围;

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

                                                                                                                     正文

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1、计划任务概说;

计划任务,是任务在约定的时间执行已经计划好的工作,这是表面的意思。在Linux中,我们经常用到 cron 服务器来完成这项工作。cron服务器可以根据配置文件约定的时间来执行特定的作务。比如我们可以在配置文件中约定每天早上4点,对httpd 服务器重新启动,这就是一个计划任务;

2cron

Linux系统中,计划任务一般是由cron承担,我们可以把cron设置为开机时自动启动。cron启动后,它会读取它的所有配置文件(全局性配置文件/etc/crontab,以及每个用户的计划任务配置文件),然后cron会根据命令和执行时间来按时来调用度工作任务。

2.1 cron 使用方法;

启动:root@ubuntu:/home/linuxer# service cron start

停止:root@ubuntu:/home/linuxer# service cron stop

重启:root@ubuntu:/home/linuxer# service cron restart

查看状态:root@ubuntu:/home/linuxer# service cron status

重新载入配置:root@ubuntu:/home/linuxer# service cron reload

2.2 编辑cron 配置文件;

cron 是一个服务器程序,我们都知道Linux的服务器的配置,大多是通过配置文件来完成的,cron自然也不例外,在ubuntu等系统中它的全局性配置文件是/etc/crontab。每个用户也有自己的cron配置文件,我们可以通过crontab -e 来编辑它;

2.2.1编辑用户自身配置文件;

每个用户都有自己的cron配置文件,通过crontab -e 就可以编辑,我们编辑好用户的cron配置文件保存退出后,系统会自动就存放于/var/spool/cron/crontabs/目录中,文件以用户名命名。

crontab -u  设定某用户的cron服务

crontab -e  编辑用户的cron配置文件;

crontab -l  查看用户的计划任务;

crontab -r  删除用户的计划任务;

如:设置linuxer用户的cron配置文件

root@ubuntu:/home/linuxer# crontab -u linuxer -e

执行该命令之后,选择vim打开,文件开头的帮助说明会向你介绍其中的cron配置文件书写格式的:

# Edit this file to introduce tasks to be run by cron.

# 

# Each task to run has to be defined through a single line

# indicating with different fields when the task will be run

# and what command to run for the task

# 

# To define the time you can provide concrete values for

# minute (m), hour (h), day of month (dom), month (mon),

# and day of week (dow) or use '*' in these fields (for 'any').

# 

# Notice that tasks will be started based on the cron's system

# daemon's notion of time and timezones.

# 

# Output of the crontab jobs (including errors) is sent through

# email to the user the crontab file belongs to (unless redirected).

# 

# For example, you can run a backup of all your user accounts

# at 5 a.m every week with:

# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

# 

# For more information see the manual pages of crontab(5) and cron(8)

# 

# m h  dom mon dow   command

我翻译之:
#编辑这个文件,让cron服务器能够执行其中的任务

#

#每一项任务必须通过一行来定义

#使用不同的列来说明任务的计划执行时间

#和任务的执行命令

#

#定义计划执行时间的时候,你应该提供具体的分,时,日,月和星期,或者使用‘*’代表该列为任意值

#

#注意,任务将根据cron的系统

#守护进程的时间与时区概念开始执行

#

#crontab的输出工作(包括错误信息的输出)就是通过email向拥有该crontab的用户发送

#

#例如:你可以为所有的用户定时执行一项备份任务

#执行时间为每周早上5点

# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

# 

#更多信息请查阅crontab(5)和cron(8)的man手册帮助文档

#

#分 时 日 月 星期 命令

配置好后,我们还要重新启动cron服务器,每个用户cron配置文件的改动都得重新启动crond服务器;

2.2.2 编辑全局性配置文件;

全局性配置文件类似,只要按照上面的书写格式编辑/etc/crontab文件就行,唯一不同的是全局性配置文件书写格式中添加了用户名这一列,

全局性配置文件:

# m h dom mon dow user command

用户自身配置文件

# m h  dom mon dow   command

3、计划任务的应用范围;

计划任务主要是让系统自动完成一些工作。比如我们可以让系统自动在某一时刻清理或备份httpd服务器的日志,然后重新启动httpd服务器,这对网络管理员来说是很重要的。也可以让系统自动清空位于/tmp目录的垃圾文件。



你可能感兴趣的:(linux,ubuntu,服务器,command,cron,任务)