Skip to content

Commit

Permalink
advanced/daemon: add cron
Browse files Browse the repository at this point in the history
  • Loading branch information
sscscc committed Feb 15, 2024
1 parent 56e085f commit 8e03e60
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
92 changes: 92 additions & 0 deletions docs/advanced/daemon/cron.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Cron

Cron是一个基于"定时"的作业调度器,在Unix-like操作系统中广泛使用。它允许用户安排在特定时间或周期性执行的作业,例如自动备份、系统更新和定期清理等任务。Cron在后台作为守护进程运行,并检查/etc/crontab文件、/etc/cron.*目录和用户的crontab文件以执行预定的任务,在指定的时间执行它们。

在本手册中,我们将详细介绍如何使用crontab文件配置任务,包括语法、时间参数的设置以及如何管理和排除故障cron作业。

每个用户都可以有自己的crontab文件,而/etc/crontab文件则是系统级的。用户可以通过crontab命令编辑自己的作业调度。

## 编辑Crontab

要编辑或创建用户的crontab文件,可以使用以下命令:

```

Check failure on line 13 in docs/advanced/daemon/cron.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should have a language specified

docs/advanced/daemon/cron.md:13 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: "```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md040.md

Check failure on line 13 in docs/advanced/daemon/cron.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should have a language specified

docs/advanced/daemon/cron.md:13 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: "```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md040.md
crontab -e
```

Crontab文件的每一行都代表一个作业,格式如下:

```

Check failure on line 19 in docs/advanced/daemon/cron.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should have a language specified

docs/advanced/daemon/cron.md:19 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: "```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md040.md

Check failure on line 19 in docs/advanced/daemon/cron.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should have a language specified

docs/advanced/daemon/cron.md:19 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: "```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md040.md
* * * * * command_to_execute
```

这五个星号代表不同的时间单位:

- 第一个星号的部分代表分钟(0到59)

- 第二个星号的部分代表小时(0到23)

- 第三个星号的部分代表一个月中的日期(1到31)

- 第四个星号的部分代表月份(1到12)

- 第五个星号的部分代表星期几(0到7,其中0和7都代表星期日)

每个字段可以使用以下字符来指定不同的时间:

- 星号(*):表示任何可能的值,例如,在小时字段中使用星号意味着"每个小时"。

- 逗号(,):允许列举多个值,例如,1,3,5表示"在1点、3点和5点"。

- 中划线(-):表示一个范围,例如2-4表示"2点到4点"。

- 正斜杠(/):表示步长,*/2在小时字段中使用表示"每两个小时"

## 示例

1. 每天午夜12点运行 `backup.sh` 脚本:

```

Check failure on line 49 in docs/advanced/daemon/cron.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should have a language specified

docs/advanced/daemon/cron.md:49 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: " ```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md040.md

Check failure on line 49 in docs/advanced/daemon/cron.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should have a language specified

docs/advanced/daemon/cron.md:49 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: " ```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md040.md
0 0 * * * /path/to/backup.sh
```

2. 每个工作日的上午9点到下午5点,每小时的第30分钟运行 `check.sh` 脚本:

```

Check failure on line 55 in docs/advanced/daemon/cron.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should have a language specified

docs/advanced/daemon/cron.md:55 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: " ```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md040.md

Check failure on line 55 in docs/advanced/daemon/cron.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should have a language specified

docs/advanced/daemon/cron.md:55 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: " ```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md040.md
30 9-17 * * 1-5 /path/to/check.sh
```

3. 每两个小时运行一次 `sync.sh` 脚本:

```

Check failure on line 61 in docs/advanced/daemon/cron.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should have a language specified

docs/advanced/daemon/cron.md:61 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: " ```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md040.md

Check failure on line 61 in docs/advanced/daemon/cron.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should have a language specified

docs/advanced/daemon/cron.md:61 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: " ```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md040.md
0 */2 * * * /path/to/sync.sh
```

## 管理Crontab


查看用户的crontab文件内容:

Check failure on line 69 in docs/advanced/daemon/cron.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should have a language specified

docs/advanced/daemon/cron.md:69 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: "```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md040.md

Check failure on line 69 in docs/advanced/daemon/cron.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should have a language specified

docs/advanced/daemon/cron.md:69 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: "```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md040.md
```
crontab -l
```

删除用户的crontab文件:

Check failure on line 75 in docs/advanced/daemon/cron.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should have a language specified

docs/advanced/daemon/cron.md:75 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: "```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md040.md

Check failure on line 75 in docs/advanced/daemon/cron.md

View workflow job for this annotation

GitHub Actions / build

Fenced code blocks should have a language specified

docs/advanced/daemon/cron.md:75 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: "```"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md040.md
```
crontab -r
```

## 故障排除

有的时候Cron任务可能没有按预期运行,可以按照以下几点检查:

- 确保Cron服务正在运行。

- 检查Cron日志(通常在/var/log/cron或通过journalctl命令查看)以了解作业执行情况。

- 确保crontab文件的命令行路径是正确的。

- 确保命令行是可执行的,并且所有的文件权限都是正确的。

- 注意Cron环境变量可能与用户环境变量不同,可能需要在脚本中显式设置环境变量。
5 changes: 5 additions & 0 deletions docs/advanced/daemon/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
!!! warning "本文仍在编辑中"

Check failure on line 1 in docs/advanced/daemon/index.md

View workflow job for this annotation

GitHub Actions / build

First line in a file should be a top-level heading

docs/advanced/daemon/index.md:1 MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "!!! warning "本文仍在编辑中""] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md041.md

Check failure on line 1 in docs/advanced/daemon/index.md

View workflow job for this annotation

GitHub Actions / build

First line in a file should be a top-level heading

docs/advanced/daemon/index.md:1 MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "!!! warning "本文仍在编辑中""] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md041.md

# 守护进程

在Linux操作系统中,守护进程(Daemon)是在后台运行的服务进程,它们通常在系统启动时启动并持续运行,直到系统关闭。守护进程在没有用户交互的情况下执行各种任务,如日志记录、系统监控、调度作业以及其他系统管理任务。
4 changes: 3 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ nav:
- DuckDB: advanced/duckdb.md
- Sentry: advanced/sentry.md
- Nmap: advanced/nmap.md
- 守护进程: advanced/daemon.md
- 守护进程:
- advanced/daemon/index.md
- cron: advanced/daemon/cron.md
- 超算: advanced/supercomputer.md
- 网管交换机简介: advanced/switch.md

0 comments on commit 8e03e60

Please sign in to comment.