如何在Linux上计划任务:Crontab文件简介

The cron daemon on Linux runs tasks in the background at specific times; it’s like the Task Scheduler on Windows. Add tasks to your system’s crontab files using the appropriate syntax and cron will automatically run them for you.

Linux上的cron守护程序在特定时间在后台运行任务; 就像Windows上的Task Scheduler。 使用适当的语法将任务添加到系统的crontab文件中,cron会自动为您运行它们。

Crontab files can be used to automate backups, system maintenance and other repetitive tasks. The syntax is powerful and flexible, so you can have a task run every fifteen minutes or at a specific minute on a specific day every year.

Crontab文件可用于自动执行备份,系统维护和其他重复性任务。 语法功能强大且灵活,因此您可以在每年的特定日期每隔15分钟或在特定分钟运行一次任务。

打开Crontab (Opening Crontab)

First, open a terminal window from your Linux desktop’s applications menu. You can click the Dash icon, type Terminal and press Enter to open one if you’re using Ubuntu.

首先,从Linux桌面的应用程序菜单中打开一个终端窗口。 如果使用的是Ubuntu,则可以单击Dash图标,键入Terminal并按Enter打开一个。

Use the crontab -e command  to open your user account’s crontab file. Commands in this file run with your user account’s permissions. If you want a command to run with system permissions, use the sudo crontab -e command to open the root account’s crontab file. Use the su -c “crontab -e” command instead if your Linux distribution doesn’t use sudo.

使用crontab -e命令打开用户帐户的crontab文件。 该文件中的命令在您的用户帐户权限下运行。 如果要使用系统权限运行命令,请使用sudo crontab -e命令打开根帐户的crontab文件。 如果您Linux发行版不使用sudo,请改用su -c“ crontab -e”命令。

You may be asked to select an editor. Select Nano if it’s available by typing its number and pressing Enter. Vi and other more advanced editors may be preferred by advanced users, but Nano is an easy editor to get started with.

可能会要求您选择一个编辑器。 通过键入其编号并按Enter键选择Nano(如果可用)。 Vi和其他更高级的编辑器可能会受到高级用户的青睐,但是Nano是一个易于入门的编辑器。

You’ll see the Nano text editor, identified by the “GNU nano” header at the top of your terminal window. If you don’t, crontab probably opened in the vi text editor.

您会在终端窗口顶部看到Nano文本编辑器,由“ GNU nano”标题标识。 如果您不这样做,则crontab可能会在vi文本编辑器中打开。

If you’re not comfortable using vi, you can type :quit into vi and press Enter to close it. Run the export EDITOR=nano command, then run crontab -e again to open the crontab file in Nano.

如果您不习惯使用vi,可以在Vi中键入:quit并按Enter将其关闭。 运行export EDITOR = nano命令,然后再次运行crontab -e在Nano中打开crontab文件。

添加新任务 (Adding New Tasks)

Use the arrow keys or the page down key to scroll to the bottom of the crontab file in Nano. The lines starting with # are comment lines, which means that cron ignores them. Comments just provide information to people editing the file.

使用箭头键或向下翻页键滚动到Nano中crontab文件的底部。 以#开头的行是注释行,这意味着cron会忽略它们。 注释仅向编辑文件的人员提供信息。

Lines in the crontab file are written in the following sequence, with the following acceptable values:

crontab文件中的行按以下顺序编写,并具有以下可接受的值:

minute(0-59) hour(0-23) day(1-31) month(1-12) weekday(0-6) command

分钟(0-59)小时(0-23)日(1-31)月(1-12)工作日(0-6)命令

You can use an asterisk (*) character to match any value. For example, using a asterisk for the month would cause the command to run every month.

您可以使用星号(*)字符来匹配任何值。 例如,在月份中使用星号会使命令每月运行一次。

For example, let’s say we want to run the command /usr/bin/example at 12:30 a.m. every day. We’d type:

例如,假设我们想每天在12:30运行命令/ usr / bin / example。 我们输入:

29 0 * * * /usr/bin/example

29 0 * * * / usr / bin / example

We use 29 for the 30-minute mark and 0 for 12 a.m. because the minute, hour and weekday values start at 0. Note that the day and month values start at 1 instead of 0.

30分钟标记使用29,上午12点使用0,因为分钟,小时和星期几的值从0开始。请注意,日期和月份的值从1而不是0开始。

多个值和范围 (Multiple Values and Ranges)

Use comma-separated values to specific multiple times. For example, the line

多次使用逗号分隔的值。 例如,线

0,14,29,44 * * * * /usr/bin/example2

0,14,29,44 * * * * / usr / bin / example2

runs /usr/bin/example2 at the 15-minute mark on every hour, every day. Make sure you add each new task on a new line.

每天每天的15分钟运行/ usr / bin / example2。 确保在新行上添加每个新任务。

Use dash-separated values to specify a range of values. For example, the line

使用破折号分隔的值来指定值的范围。 例如,线

0 11 * 1-6 * /usr/bin/example3

0 11 * 1-6 * / usr / bin / example3

runs /usr/bin/example3 at noon every day, but only in the first six months of the year.

每天中午运行/ usr / bin / example3,但仅在一年的前六个月运行。

保存文件 (Saving the File)

Press Ctrl-O and press Enter to save the crontab file in Nano. Use the Ctrl-X shortcut to close Nano after you’ve saved the file.

按Ctrl-O并按Enter将crontab文件保存在Nano中。 保存文件后,使用Ctrl-X快捷方式关闭Nano。

You’ll see the “crontab: installing new crontab” message, indicating that your new crontab file was installed successfully.

您将看到“ crontab:安装新的crontab”消息,表明您的新crontab文件已成功安装。

翻译自: https://www.howtogeek.com/101288/how-to-schedule-tasks-on-linux-an-introduction-to-crontab-files/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值