博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cron和crontab_Linux crontab命令创建和管理Cron作业
阅读量:2533 次
发布时间:2019-05-11

本文共 5055 字,大约阅读时间需要 16 分钟。

cron和crontab

Linux crontab command is used to schedule cron jobs. A cron job is a handy automation tool that enables Linux users to execute automated tasks at specific periods or intervals.

Linux crontab命令用于调度cron作业。 cron作业是一种便捷的自动化工具,使Linux用户可以在特定时间段或间隔执行自动化任务。

This is a friendly way of scheduling tasks which would otherwise be tedious if managed manually. Cron jobs are usually used by system administrators to automate tasks such as server backups, restarts, sending of notifications, and clean-ups to mention just but a few.

这是一种调度任务的友好方式,如果手动进行管理,这将是乏味的。 系统管理员通常使用Cron作业来自动化任务,例如服务器备份,重新启动,发送通知以及清理(仅举几例)。

A cron job runs silently in the background, checking the /etc/crontab file and /var/spool/cron and /etc/cron.*/ directories. In this article, you will learn how to create and manage cron jobs in your Linux system.

cron作业在后台静默运行,检查/etc/crontab文件以及/var/spool/cron/etc/cron.*/目录。 在本文中,您将学习如何在Linux系统中创建和管理cron作业。

Linux Cron工作结构 (Linux Cron Job Structure)

The anatomy of a cron job is as shown below.

Cron作业的解剖结构如下所示。

* * * * * COMMAND arg1 arg2

OR

要么

* * * * * /path/to/script arg1 arg2

From the left,

从左边开始

The 1st entry represents Minutes [0 - 59]The 2nd entry represents Hours   [0 - 24]The 3rd entry represents Days    [0 - 31]The 4th entry represents Months  [0 - 12]The 5th entry represents Days of the week [0 - 7]

Linux crontab示例 (Linux crontab Example)

Now that you have the skeleton for a crontab, writing one is quite easy provided you know the command or the path to your script.

现在您已经有了crontab的框架,只要知道命令或脚本路径,编写一个crontab就很容易了。

In this example, we are going to create a crontab for rebooting a Linux system.

在此示例中,我们将创建一个crontab来重新引导Linux系统。

vim reboot.sh

Copy and paste the content below into the bash file.

将以下内容复制并粘贴到bash文件中。

#!/bin/bash0 3 * * * /sbin/reboot

Going by the crontab syntax as earlier indicated, the cron job will run daily at 3:00 am and reboot the system.

按照前面指示的crontab语法进行操作,cron作业将每天在凌晨3:00运行并重新启动系统。

Save and quit the text editor.

保存并退出文本编辑器。

Next, assign the execute permissions as shown.

接下来,分配执行权限,如下所示。

# chmod +x reboot.sh

To verify file permissions, run:

要验证文件权限,请运行:

# ls -l reboot.sh

To initialize the crontab run

初始化crontab运行

# crontab reboot.sh

If you want to view contents of a crontab of a user that is currently logged in user run the command.

如果要查看当前登录用户的用户crontab的内容,请运行命令。

crontab -l

With the crontab up and running, the crontab will be executed at the stipulated time, i.e 3:00 am local time.

随着crontab的启动和运行,crontab将在规定的时间(即本地时间3:00)执行。

To remove all cron jobs in the system, use the -r option.

要删除系统中的所有cron作业,请使用-r选项。

crontab -r

在特定时间间隔运行cron作业的其他示例 (Additional examples of running cron jobs at certain time intervals)

  1. To run a cron job every minute, run
    * * * * * COMMAND

    If the current time is 6:00 am, the cron job will run at 6:01 am, 6:02 and so on.

    每分钟执行一次cron作业,请运行

    如果当前时间是6:00 am,则cron作业将在6:01 am,6:02等运行。

  2. To run a cron job after every nth minute, for example after every 10 minutes, run
    */10 * * * * COMMAND

    在每n分钟之后(例如,每10分钟之后)运行一次cron作业,请运行
  3. To run a cron job every nth minute of every hour, for example after every 30 minutes of every hour, execute
    30 * * * * COMMAND

    要在每小时的第n分钟执行一次cron作业,例如每小时的每30分钟执行一次,请执行
  4. To run a cron job every hour, i.e every 0 minutes, run
    0 * * * * COMMAND

    每小时 (即每0分钟)运行一次cron作业,请运行
  5. To run a cron job after every n hours, for instance, after every 6 hours, run
    0 */6 * * * COMMAND

    每n小时运行一次cron作业,例如,每6小时运行一次
  6. To run a cron job at a specific day, say Friday, execute
    0 0 * * FRI COMMAND

    在特定日期 (例如星期五)执行计划任务,请执行
  7. This cron job will be executed every Friday at exactly 00:00 midnight.

    该Cron作业将在每个星期五正午00:00执行。

  8. If you wish to run a cron job from Monday to Fridayrun
    0 0 * * 1-5 COMMAND

    This executes the command at 00:00 hours every day from Monday to Friday.

    从周一到周五运行Cron作业

    从星期一到星期五,每天每天00:00执行命令。

  9. To execute a cron job everyday, run
    0 0 * * * COMMAND

    每天执行一次cron作业,请运行
  10. To execute a cron job every first day of every month
    0 0 1 * * COMMAND

    每个月的第一天执行一次cron作业
  11. To execute a cron job every first day of the month at a specific time, say 20:30 hours, run
    30 20 1 * * COMMAND

    在每月的第一天的特定时间 (例如20:30小时)执行一次cron作业,请运行
  12. To run a job every quarter of the year, i.e. the 1st day after every 3 months, run
    0 0 1 */3 * COMMAND

    要在每年的每个季度(即每3个月后的第一天)运行一份工作,
  13. To run a cron job every year, run
    0 0 1 1 * COMMAND

    每年进行一项计划工作,

使用Crontab Guru安排cron作业 (Scheduling cron jobs using Crontab Guru)

Crontab syntaxes can be quite confusing and difficult to master. But don’t worry. is a very friendly online tool that helps you seamlessly define your time intervals without much hassle. It’s a web interface that has crontab’s time intervals well labeled and all that is required is for you to type in the figures into the text field as the tool interprets the crontab. In the example below, the tool generated the crontab for automating a cron job every 6:00 am from Monday to Saturday.

Crontab语法可能非常混乱并且难以掌握。 但是不用担心。 是一个非常友好的在线工具,可以帮助您无缝地定义时间间隔,而不会带来太多麻烦。 这是一个Web界面,上面标有crontab的时间间隔,您只需在工具解释crontab时将数字输入文本字段即可。 在下面的示例中,该工具生成了crontab,用于从星期一到星期六的每个上午6:00自动执行cron作业。

翻译自:

cron和crontab

转载地址:http://woqzd.baihongyu.com/

你可能感兴趣的文章
欧建新之死
查看>>
自定义滚动条
查看>>
APP开发手记01(app与web的困惑)
查看>>
笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)
查看>>
mysql 日期时间运算函数(转)
查看>>
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>
万方数据知识平台 TFHpple +Xpath解析
查看>>
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
vs code调试console程序报错--preLaunchTask“build”
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
端口号大全
查看>>
机器学习基石笔记2——在何时可以使用机器学习(2)
查看>>
POJ 3740 Easy Finding (DLX模板)
查看>>
MySQL 处理重复数据
查看>>
关于typedef的用法总结(转)
查看>>
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>