In Linux, tasks can be configured to run automatically within a specified period of time, on a specified date, or when the system load average is below a specified number. Red Hat Enterprise Linux is pre-configured to run important system tasks to keep the system updated. For example, the slocate database used by the locate command is updated daily. A system administrator can use automated tasks to perform periodic backups, monitor the system, run custom scripts, and more.
Red Hat Enterprise Linux comes with several automated tasks utilities: cron, at, and batch.

39.1. Cron

Cron is a daemon that can be used to schedule the execution of recurring jobs according to a combination of the time, day of the month, month, day of the week, and week.
Cron assumes that the system is on continuously. If the system is not on when a job is scheduled, it is not executed. To schedule one-time jobs, refer to Section 39.2, “At and Batch”.
To use the cron service, the vixie-cron RPM package must be installed and the crond service must be running. To determine if the package is installed, use the rpm -q vixie-cron command. To determine if the service is running, use the command /sbin/service crond status.

39.1.1. Configuring Cron Jobs

The main configuration file for cron, /etc/crontab, contains the following lines:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/ # run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
The first four lines are variables used to configure the environment in which the cron jobs are run. The SHELL variable tells the system which shell environment to use (in this example the bash shell), while the PATH variable defines the path used to execute commands. The output of the cron jobs are emailed to the username defined with the MAILTO variable. If the MAILTO variable is defined as an empty string (MAILTO=""), email is not sent. The HOME variable can be used to set the home directory to use when executing commands or scripts.
Each line in the /etc/crontab file represents a job and has the following format:
minute   hour   day   month   dayofweek   command
  • minute — any integer from 0 to 59
  • hour — any integer from 0 to 23
  • day — any integer from 1 to 31 (must be a valid day if a month is specified)
  • month — any integer from 1 to 12 (or the short name of the month such as jan or feb)
  • dayofweek — any integer from 0 to 7, where 0 or 7 represents Sunday (or the short name of the week such as sun or mon)
  • command — the command to execute (the command can either be a command such as ls /proc >> /tmp/proc or the command to execute a custom script)
For any of the above values, an asterisk (*) can be used to specify all valid values. For example, an asterisk for the month value means execute the command every month within the constraints of the other values.
A hyphen (-) between integers specifies a range of integers. For example, 1-4 means the integers 1, 2, 3, and 4.
A list of values separated by commas (,) specifies a list. For example, 3, 4, 6, 8 indicates those four specific integers.
The forward slash (/) can be used to specify step values. The value of an integer can be skipped within a range by following the range with /<integer>. For example, 0-59/2 can be used to define every other minute in the minute field. Step values can also be used with an asterisk. For instance, the value */3 can be used in the month field to run the job every third month.
Any lines that begin with a hash mark (#) are comments and are not processed.
As shown in the /etc/crontab file, the run-parts script executes the scripts in the /etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly/, and /etc/cron.monthly/ directories on an hourly, daily, weekly, or monthly basis respectively. The files in these directories should be shell scripts.
If a cron job is required to be executed on a schedule other than hourly, daily, weekly, or monthly, it can be added to the /etc/cron.d/ directory. All files in this directory use the same syntax as /etc/crontab. Refer to Example 39.1, “Sample of /etc/crontab” for examples.

Example 39.1. Sample of /etc/crontab

# record the memory usage of the system every monday
# at 3:30AM in the file /tmp/meminfo
30 3 * * mon cat /proc/meminfo >> /tmp/meminfo
# run custom script the first day of every month at 4:10AM
10 4 1 * * /root/scripts/backup.sh
Users other than root can configure cron jobs by using the crontab utility. All user-defined crontabs are stored in the /var/spool/cron/ directory and are executed using the usernames of the users that created them. To create a crontab as a user, login as that user and type the command crontab -e to edit the user's crontab using the editor specified by the VISUAL or EDITOR environment variable. The file uses the same format as /etc/crontab. When the changes to the crontab are saved, the crontab is stored according to username and written to the file /var/spool/cron/username.
The cron daemon checks the /etc/crontab file, the /etc/cron.d/ directory, and the /var/spool/cron/ directory every minute for any changes. If any changes are found, they are loaded into memory. Thus, the daemon does not need to be restarted if a crontab file is changed.
Cron jobs can be run at random intervals, which is useful for highly loaded shared networks in order to avoid overloading the network. Job randomization is disabled by default but it can be configured in the /etc/sysconfig/run-parts file by specifying the following parameters:
  • RANDOMIZE — When set to 1, it enables randomize functionality. When set to 0, cron job randomization is disabled.
  • RANDOM — Specifies the initial random seed. It has to be set to an integer value greater than or equal to 1.
  • RANDOMTIME — When set to an integer value greater than or equal to 1, it provides an additional level of randomization.

Example 39.2. Sample of /etc/sysconfig/run-parts - Job Randomization Setting

RANDOMIZE=1
RANDOM=4
RANDOMTIME=8

39.1.2. Controlling Access to Cron

The /etc/cron.allow and /etc/cron.deny files are used to restrict access to cron. The format of both access control files is one username on each line. Whitespace is not permitted in either file. The cron daemon (crond) does not have to be restarted if the access control files are modified. The access control files are read each time a user tries to add or delete a cron job.
The root user can always use cron, regardless of the usernames listed in the access control files.
If the file cron.allow exists, only users listed in it are allowed to use cron, and the cron.deny file is ignored.
If cron.allow does not exist, users listed in cron.deny are not allowed to use cron.

39.1.3. Starting and Stopping the Service

To start the cron service, use the command /sbin/service crond start. To stop the service, use the command /sbin/service crond stop. It is recommended that you start the service at boot time. Refer to Chapter 18, Controlling Access to Services for details on starting the cron service automatically at boot time.

RedHat 的 crontab的更多相关文章

  1. Linux 每天自动备份mysql数据库的方法

    Linux 每天自动备份mysql数据库的方法 作者: 字体:[增加 减小] 类型:转载   linux下为了安全有时候需要自动备份mysql数据库,下面是具体的实现步骤.   /usr/bin为my ...

  2. 《使用shell位置变量进行目录文件的备份小脚本》

    今天才发现原来位置变量也可以玩的这么爽!! 这是使用位置变量进行文件目录备份:#!/bin/bashDATE=`date +%F`  //日期以年月日输出tar czf $1.$DATE.tar.gz ...

  3. Linux服务器自动备份压缩MySQL数据库的实用方法

    <?php$server = 'localhost'; $link = mysql_connect($server, 'root', 'haven'); $result = mysql_quer ...

  4. 让linux每天定时备份MySQL数据库并删除五天前的备份文件

    MYSQL定期备份是一项重要的工作,但人工操作太繁琐,也难避免有所疏漏,使用下面的方法即可让系统定期备份数据.利用系统crontab来定时执行备份文件,按日期对备份结果进行保存,达到备份的目的. 1. ...

  5. linux定时备份MySQL数据库并删除七天前的备份文件

    1.创建备份文件夹 #cd /bak#mkdir mysqldata 2.编写运行脚本 #nano -w /usr/sbin/bakmysql.sh 注:如使用nano编辑此代码需在每行尾添加’&am ...

  6. 程序员(媛)Shell脚本必备技能之中的一个: 在Linux下怎样自己主动备份mysql数据

    作者:荀彧(花名) Background:鉴于QAS数据库曾遭到毁灭性损害.极其桑心L.为了避免这样的玉石俱焚的状况再度发生.于是我们找出了一种应对方案.这里给大家分享一下怎样实现定时备份mysql数 ...

  7. crontab小结

    crontab是linux下的计划任务,可以用来定时或者按计划运行命令. 创建计划任务: 1.使用crontab -e命令,直接创建计划任务 2.使用编辑器编写好计划任务的文件后,再使用crontab ...

  8. crontab执行java命令失效

    一.我们常常碰到在shell下执行某个命令能够成功,比如执行一个java程序: java -jar /home/opscoder/topo-audit.jar,但是在crontab下执行会失败. co ...

  9. linux crontab定时执行shell脚本

    linux下使用crontab命令被用来提交和管理用户的需要周期性执行的任务,示例如下:crontab -e 编辑周期任务30 21 * * * /etc/init.d/smb restart 每晚的 ...

随机推荐

  1. 1.Metasploit介绍与基本命令

    Metasploit体系框架介绍 Metasploit是目前世界上领先的渗透测试工具,也是信息安全与渗透测试领域最大的开源项目之一.它彻底改变了我们执行安全测试的方式. Metasploit之所以流行 ...

  2. E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)

    E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...

  3. ML-Agents(六)Tennis

    目录 ML-Agents(六)Tennis 一.Tennis介绍 二.环境与训练参数 三.场景基本结构 四.代码分析 环境初始化脚本 Agent脚本 Agent初始化与重置 矢量观测空间 Agent动 ...

  4. Shell:Day10

    shell脚本:明白一点:shell脚本本身是一个工具 在写shell脚本之前,就要明白:这个功能能到底如何实现? curl 访问文件源代码,查看网站状态: 才能通过shell(bash)所提供的逻辑 ...

  5. Python等同于PHP的 strip_tags?

    我感觉目前最好的方式 from django.utils.html import strip_tags

  6. MyBatis(九):动态SQL

    本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接 https://space.bilibili.com/95256449?spm_id_from=33 ...

  7. PHP如何配置session存储在redis

    当网站用户量增多的时候,正常的session存取就会出现有点慢的问题,如果提高速度呢. 我们可以使用redis去保存session的会话信息. PHP的会话默认是以文件的形式存在的,可以配置到NoSQ ...

  8. Vue-cli2.0 第3节 解读Vue-cli模板

    Vue-cli2.0 第3节 解读Vue-cli模板 目录 Vue-cli2.0 第3节 解读Vue-cli模板 第3节 解读Vue-cli模板 1. npm run build命令 2. main. ...

  9. 接口请求:get、post (requests方法)

    https://www.cnblogs.com/lanyinhao/p/9634742.html 比较全面 1.模块说明 requests是使用Apache2 licensed 许可证的HTTP库. ...

  10. 在SpringBoot中使用SpringSecurity

    @ 目录 提出一个需求 解决方案: 使用SpringSecurity进行解决 SpringSecurity和SpringBoot结合 1. 首先在pom.xml中引入依赖: 2. 配置用户角色和接口的 ...