惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
AWS News Blog
AWS News Blog
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
美团技术团队
T
Threatpost
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
Blog — PlanetScale
Blog — PlanetScale
C
Cybersecurity and Infrastructure Security Agency CISA
F
Full Disclosure
博客园_首页
N
Netflix TechBlog - Medium
Security Latest
Security Latest
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Announcements
Recent Announcements
博客园 - Franky
P
Palo Alto Networks Blog
Project Zero
Project Zero
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
GbyAI
GbyAI

Linux – Ed_'s Blog

给 Debian 开了自动安全更新 – yywr's Blog UPower – 将笔记本电池当成UPS使用 – yywr's Blog Samba 服务基本用法 – yywr's Blog Linux 程序开机启动并保活:Systemd 进程守护管理工具 – yywr's Blog Linux 下自动挂载 Samba /CIFS 共享文件夹 – yywr's Blog Ubuntu 开机自动挂载第二块硬盘 – yywr's Blog 重装了家里的服务器系统 – yywr's Blog Ubuntu 笔记本合盖子不休眠的方法 – yywr's Blog
Crontab 使用笔记 – yywr's Blog
yywr · 2023-08-16 · via Linux – Ed_'s Blog

很不想搬运这篇内容,感觉没什么意义,但每次想要用的时候都要去搜,然后点几次链接才能找到想要的内容,所以还是抄一份放这里备用吧

为了便利的工作,Windows 下的计划任务倒是用得多点,而且配置项还算丰富,很多需求都能够得到满足,Linux 下 corntab 目前主要就做两件事情,一个是备份站点项目文件,一个是更新 DDNS 记录

命令与参数

 crontab [ -u user ] { -e | -l | -r }
 -u : user 指定用户,默认当前用户,用它指定用户需要 sudo 提权
 -e : edit 编辑任务
 -l : list 列出用户下的任务
 -r : remove 删除用户下的任务,我觉得有 -e 就够了

任务格式

这是搬运这文章的原因,每次都要想一下它这个时间设置是怎么个格式法,它简洁、灵活,但每次都用的时候都记忆模糊

*    *    *    *    *
- - - - -
| | | | |
| | | | +----- 星期中星期几 (0 |- 6) (星期天 为0)
| | | +---------- 月份 (1 - 12)
| | +--------------- 一个月中的第几天 (1 - 31)
| +-------------------- 小时 (0 - 23)
+------------------------- 分钟 (0 - 59)

星号()是表示当前对应时间单位内里的每一个颗粒度,可以指定有效的时间点,也可以使用 – 号来选定范围,还可以使用 / 号来表示间隔,比如 “ */30 ” 可以表示每隔30分钟,当然它们也可以组合使用

使用方法

使用 crontab -e 来编辑当前用户的任务清单文件,在文件的最后面按上面的格式添加一条记录,保存退出即可(有些系统会调用 vim 有些会调用 nano)

先是按上面格式设置执行的时间,然后接运行的命令,一般是 bash + 脚本完整路径,比如:

#每30分钟跑一次DDNS的脚本
*/30 * * * * bash /home/usr/Config/DDNS-update.sh

 注意事项:不知道是因为环境变量还是其它什么原因,在当以 .sh脚本作执行动作时,最好在路径前加上 bash,我碰到过不加不运行的情况

其它可能需要知道的

守护进程: crond (Ubuntu 下名字是 cron.service) ,如果小概率发生任务未执行的情况,尝试用 systemctl 看下它还活没活

系统任务:我们编辑的是用户 任务,有系统任务,保存在 /etc/crontab ,不管它

主要文件:

  • /var/spool/cron/crontabs/ – 用户任务清单保存目录,以用户名命名
  • /etc/cron.allow – 允许使用crontab 的用户,一般没这个文件,按需要自己建议,里面填用户名就行,各占一行
  • /etc/cron.deny – 禁止使用crontab 的用户,一般 guest 用户是在列的
  • /etc/cron.daily – 一般是程序自动创建的任务,可能还有 cron.monthly / hourly 等,自行打 cron 后按 tab 键补全查看

参考:

本文是 居家服务器折腾笔记 的一部分