温馨提示×

如何编辑Ubuntu中的crontab文件

小樊
88
2024-08-21 15:18:33
栏目: 智能运维

要编辑Ubuntu中的crontab文件,请按照以下步骤进行:

  1. 打开终端:您可以通过按下Ctrl + Alt + T来打开终端。

  2. 输入以下命令以编辑您的用户crontab文件:

crontab -e
  1. 如果您是第一次编辑crontab文件,系统可能会询问您选择一个文本编辑器。请选择您熟悉的编辑器,比如nano或vim。

  2. 您现在应该看到一个类似于以下内容的文件:

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
  1. 在文件中添加您想要运行的任务。每行代表一个任务。每行的格式如下:
* * * * * /path/to/command

其中,星号代表任意时间,您可以根据您的需求修改各个字段的值,以定义您要运行任务的具体时间。

  1. 编辑完毕后,请按Ctrl + O保存更改,然后按Ctrl + X退出编辑器。

  2. 您的crontab文件已经成功编辑并保存。更改将会立即生效。

请注意,如果您想编辑系统范围的crontab文件,可以使用以下命令:

sudo crontab -e

0