首页 > 系统相关 >CentOS 定时任务 Crontab

CentOS 定时任务 Crontab

时间:2023-01-01 23:45:13浏览次数:55  
标签:CentOS crontab month cron etc Crontab 定时 user

一、环境

1. CentOS 7.9

2. Crontab

Crontab是Linux下的一个执行定时任务的守护进程(daemon),它有一个文件里面维护着所有需要定时执行的任务条目。

二、安装

1. 安装

默认情况下CentOS7已经安装了Crontab:

rpm -qa | grep crontab

2. 服务

Crontab的服务名称是crond

(1) 状态

systemctl status crond

三、配置

1. 配置目录

(1) master

在/etc目录下,有4个/etc/cron.*目录,它们分别是:

目录 执行时间
/etc/cron.hourly 每小时的第一分钟
/etc/cron.daily 每天3:05 AM到10.55 PM之间
/etc/cron.weekly 上次执行后第7天的3:25 AM到11:10 PM之间
/etc/cron.monthly 上次执行后一个月的3:45 AM到11:30 PM之间

存放于上述4个目录中脚本将会分别按照上表中的执行时间执行。

(2) user

用户级别的配置文件位于/var/spool/cron/目录下。

2. crontab命令

Usage:
 crontab [options] file
 crontab [options]
 crontab -n [hostname]

Options:
 -u [user]  define user
 -e         edit user's crontab or create one if it doesn’t already exist
 -l         list user's crontab
 -r         delete user's crontab
 -i         prompt before deleting
 -n [host]  set host in cluster to run users' crontabs
 -c         get host in cluster to run users' crontabs
 -s         selinux context
 -x [mask]  enable debugging

3. crontab配置文件

(1) 使用crontab最简单的方式是运行:

sudo crontab -e

(2) 在打开的配置文件中编辑crontab条目,条目的语法为

{minute} {hour} {day of month} {month} {day of week} [user-name] [command to be executed]

(3) 前五个参数为定时任务执行的时间参数,它们的取值如下:

field          allowed values
-----          --------------                  
minute         0-59                  
hour           0-23                  
day of month   0-31                  
month          0-12 (or names, see below)                  
day of week    0-7 (0 or 7 is Sun, or use names)

时间参数可以是*,*代表任意时间,比如

0 1 * * *

代表每天1点执行。

(4) 例子

# 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
 |  |  |  |  |
 0  1  *  *  *  [user-name] [command to be executed]

例1: 每天00:05分执行脚本

5 0 *  *  *     /home/oracle/scan_asm_devices.sh

例2: 每月第一天的17:30分执行命令

30 17 1  *  *   mail -s "It's 5:30pm"

例3: 每周一的04:05分执行echo命令

5  4  *  *  mon echo "run at 5 after 4 every monday"

例4:每分钟执行脚本

* * * * *  /home/scripts/test.php

4. 访问控制

Crontab有两个配置文件控制可以使用crontab的用户,它们分别是:

/etc/cron.allow 允许文件中的用户使用crontab;

/etc/cron.deny 禁止文件中的用户使用crontab;

默认情况下,仅有空的/etc/cron.deny文件存在,如果两个文件都不存在,则仅允许root用户使用crontab。

如果一个用户同时出现在/etc/cron.allow和/etc/cron.deny中,则/etc/cron.deny将会被忽略。

5. 日志

可以通过以下命令查看crontab的日志:

cat /var/log/cron
tail -f /var/log/cron

四、参考

https://www.adminschoice.com/crontab-quick-reference

https://www.thegeekdiary.com/centos-rhel-begginners-guide-to-cron/

http://lampblogs.com/blog/how-to-schedule-cron-jobs-with-crontab-in-centos-7

https://linuxhint.com/a-beginners-guide-to-crontab-on-centos/

标签:CentOS,crontab,month,cron,etc,Crontab,定时,user
From: https://www.cnblogs.com/eagle6688/p/17019244.html

相关文章

  • centos7磁盘挂载
    一、新磁盘挂载1、查看磁盘#查看磁盘目录情况df-h2、查看挂载磁盘情况fdisk-l#查看设备挂载情况lsblk#默认是树形方式显示3、磁盘分区#分区命令fdisk/dev......
  • Spring Boot 定时任务 @Scheduled(cron="[秒] [分] [小时] [日] [月] [周] [年]")
    @Scheduled(cron="[秒][分][小时][日][月][周][年]")说明:多个并列的时间以英文逗号“,”隔开。比如:@Scheduled(cron="053,55161**")上面意思是:1号的下......
  • CentOS7 安装pip
    CentOS7安装pip 1、安装epel-releaseyum-yinstallepel-release 2、安装python-pipyum-yinstallpython-pip 3、查看PIP版本pip--version 4、......
  • centos8使用kubeadm搭建高可用k8s集群
    kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具。这个工具能通过两条指令完成一个kubernetes集群的部署:#创建一个Master节点kubeadminit#将一个......
  • Centos7上使用jenkins执行pipeline在容器内部拉取环境运行代码的各种坑
    环境Centos7上部署了三个服务bitbucket192.168.1.156:7900jenkins192.168.1.156:8080docker服务原理在Dockerfile中定义pipelinescripts,然后拉取dockerhub的镜像作......
  • vmware 安装centos7
    1.软硬件准备软件:推荐使用VMware,我用的是VMware12镜像:CentOS6,如果没有镜像可以在阿里云下载 https://mirrors.aliyun.com/centos/   硬件:因为是在宿主机上......
  • Eolink神技之五、API自动化——定时任务
    Eolink神技之五、API自动化——定时任务目录​​Eolink神技之五、API自动化——定时任务​​​​前言​​​​演示步骤​​​​一、项目创建​​​​1.1选择API自动化测试功......
  • centos安装mariadb
    新手,以前完全没玩过,走了很多弯路,感觉有必要做个笔记备忘开发互联网项目首先想到的数据库一般就会是mysql,以及他的衍生数据库,包括很多有经验的朋友也会给你推荐使用mysql(......
  • 定时器
    #include<stdio.h>#include<string.h>#include<unistd.h>#include"esp_timer.h"#include"esp_log.h"#include"esp_sleep.h"#include"sdkconfig.h"staticv......
  • CentOS7 执行yum 命令出错 One of the configured repositories
    新安装的一台物理服务器,执行yum命令出现了错误,[root@localhostdata]#yum-yinstallbc已加载插件:fastestmirrorLoadingmirrorspeedsfromcachedhostfileOn......