本文共 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作业。
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]
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
* * * * * 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等运行。
*/10 * * * * COMMAND要在每n分钟之后(例如,每10分钟之后)运行一次cron作业,请运行
30 * * * * COMMAND要在每小时的第n分钟执行一次cron作业,例如每小时的每30分钟执行一次,请执行
0 * * * * COMMAND要每小时 (即每0分钟)运行一次cron作业,请运行
0 */6 * * * COMMAND要每n小时运行一次cron作业,例如,每6小时运行一次
0 0 * * FRI COMMAND要在特定日期 (例如星期五)执行计划任务,请执行
This cron job will be executed every Friday at exactly 00:00 midnight.
该Cron作业将在每个星期五正午00:00执行。
0 0 * * 1-5 COMMAND
This executes the command at 00:00 hours every day from Monday to Friday.
从周一到周五运行Cron作业从星期一到星期五,每天每天00:00执行命令。
0 0 * * * COMMAND要每天执行一次cron作业,请运行
0 0 1 * * COMMAND每个月的第一天执行一次cron作业
30 20 1 * * COMMAND要在每月的第一天的特定时间 (例如20:30小时)执行一次cron作业,请运行
0 0 1 */3 * COMMAND要在每年的每个季度(即每3个月后的第一天)运行一份工作,
0 0 1 1 * COMMAND要每年进行一项计划工作,
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/