0Pricing
Linux Command Line Mastery · 课时

任务调度:`cron` 和 `at`

使用 `cron` 任务和 `at` 命令,让任务在指定时间或按指定间隔自动运行。

任务调度:`cron` 和 `at` 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Automating Your Linux Tasks

In Linux, you often need to run tasks automatically. This could be anything from backing up files to generating reports.

Automation saves time and ensures tasks are performed consistently, even when you're not actively at the keyboard.

Introducing `cron` for Recurring Jobs

cron is a powerful utility that helps you schedule commands or scripts to run automatically at specific intervals. Think of it as your personal assistant for repetitive tasks!

These scheduled tasks are often called "cron jobs".

Your Personal `crontab` File

Each user has their own crontab file, which lists their scheduled cron jobs.

  • To edit your crontab: crontab -e
  • To list your current jobs: crontab -l
  • To remove all your jobs: crontab -r (use with caution!)

When you edit, it typically opens in a text editor like Nano or Vim.

crontab -e

Understanding `cron` Job Structure

A cron job entry has six parts:

minute hour day_of_month month day_of_week command_to_execute
  • Minute: 0-59
  • Hour: 0-23 (0 is midnight)
  • Day of Month: 1-31
  • Month: 1-12 (or Jan-Dec)
  • Day of Week: 0-7 (0 or 7 is Sunday)

An asterisk (*) means "every" value for that field.

Practical `cron` Examples

Let's look at some common cron job examples you might add to your crontab file.

# Run a script every day at 3:00 AM
0 3 * * * /home/user/daily_backup.sh

# Run a command every Sunday at 10:30 PM
30 22 * * 0 echo "Weekly check!" >> /var/log/weekly.log

# Run every 15 minutes
*/15 * * * * /usr/bin/some_utility

Handy `cron` Shortcuts

For common schedules, cron offers special keywords:

  • @reboot: Run once after system startup.
  • @hourly: Run once an hour.
  • @daily: Run once a day (midnight).
  • @weekly: Run once a week (midnight Sunday).
  • @monthly: Run once a month (midnight on 1st).
  • @yearly: Run once a year (midnight on Jan 1st).

These keywords replace the first five time fields.

Scheduling Single Tasks with `at`

While cron is for recurring tasks, the at command is perfect for scheduling a command to run only once at a specified future time.

It's useful for tasks that don't need to repeat, like running a report at the end of the day or a specific cleanup task.

How to Use the `at` Command

To schedule a task with at, you provide the time and then type the command(s) you want to run. Press Ctrl+D to finish.

Here's how to schedule a simple message to appear in 1 minute:

echo "echo 'Task completed by at!'" | at now + 1 minute

Managing Your `at` Queue

You can manage your scheduled at jobs:

  • To list all pending at jobs: atq
  • To remove a specific job: atrm [job_number]

Each job listed by atq will have a unique job number.

# List all pending 'at' jobs
atq

# Remove the job with ID 42
atrm 42

`cron` & `at` Quick Check

Consider the following scenario:

You need to schedule a script named /opt/cleanup.sh to run every Tuesday at 4:45 AM.

Which crontab entry correctly achieves this?

Recap: `cron` and `at`

Great job! You've learned how to automate tasks on Linux:

  • cron is for scheduling recurring jobs using a `crontab` file and specific time syntax.
  • at is for scheduling commands to run once at a future time.

These tools are essential for system administration and keeping your systems running smoothly without constant manual intervention.

常见问题解答

「任务调度:`cron` 和 `at`」课时是免费的吗?

是的 — 「任务调度:`cron` 和 `at`」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。

「任务调度:`cron` 和 `at`」这节课中我会学到什么?

使用 `cron` 任务和 `at` 命令,让任务在指定时间或按指定间隔自动运行。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Linux Command Line Mastery 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「任务调度:`cron` 和 `at`」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?

能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 任务调度:`cron` 和 `at`
  2. 服务管理:`systemctl`(systemd)
  3. 使用 Shell 函数自动化任务
  4. 使用 journald 与 logrotate 集中记录日志并轮换
← 返回 Linux Command Line Mastery