xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Linux Schedule Cron & crontab All In One

Linux Schedule Cron & crontab All In One

定时任务 / 定时器

https://en.wikipedia.org/wiki/Cron

POSIX cron syntax

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/crontab.html#tag_20_25_07

https://crontab.guru/

# 标准 cron ✅, 通用
3 * * * *

image

# 非标准 cron ⚠️, 不通用
# Non standard! May not work with every cron.
0/3 * * * *

image

# 标准 cron ✅, 通用 🚀
*/3 * * * *

image

https://crontab.guru/every-3-minutes

https://crontab.guru/examples.html

Vixie Cron

Vixie Cron, an open source implementation of POSIX Cron, later imported into BSD and Linux

https://github.com/vixie/cron

bug

https://github.com/vixie/cron/issues/13

crontab 类型

  1. 用户级 crontab, 不需要设置用户名
# pi
$ crontab -l

# crontab 当前登录用户,无需指定用户名 ✅
$ crontab -e
# root
$ sudo crontab -l

# crontab 当前登录用户,无需指定用户名 ✅
$ sudo crontab -e

  1. 系统级 /etc/crontab, 需要指定用户名
# system
$ cat /etc/crontab

# 系统 /etc/crontab,需要指定用户名 ✅ pi
$ sudo vim /etc/crontab


# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

# /etc/crontab 系统 crontab

# 指定用户名 ✅ pi 
# user python 参数 ✅ 3 
# python 绝对路径 ✅ /home/pi/Desktop/gpio-system.py
# 二合一输出 log 🚀  2>&1
# 每一个小时的第 5 分钟执行一次,即一天 24 小时会执行 24 次 ⏰
5 * * * * pi /usr/bin/python /home/pi/Desktop/gpio-system.py 3 > /home/pi/Desktop/gpio-system.log.txt 2>&1

pi@raspberrypi:~/Desktop $ cat /etc/cron.hourly
cat: /etc/cron.hourly: 是一个目录

pi@raspberrypi:~/Desktop $ cd /etc/cron.hourly

pi@raspberrypi:/etc/cron.hourly $ ls
fake-hwclock

pi@raspberrypi:/etc/cron.hourly $ cat fake-hwclock 
#!/bin/sh
#
# Simple cron script - save the current clock periodically in case of
# a power failure or other crash

if (command -v fake-hwclock >/dev/null 2>&1) ; then
  fake-hwclock save
fi
pi@raspberrypi:/etc/cron.hourly $ 
#!/bin/sh
#
# Simple cron script - save the current clock periodically in case of
# a power failure or other crash

if (command -v fake-hwclock >/dev/null 2>&1) ; then
  fake-hwclock save
fi

https://www.cnblogs.com/xgqfrms/p/17306951.html#5167317

steps 分步执行 ✅

image

minutes (0 ~ 59)

# crontab 当前登录用户,无需指定用户名 ✅
# 从 0 分钟开始,每隔 5 分钟执行一次;
# 即第 5 分钟、第 10 分钟、第 15 分钟、第 20 分钟、第 25 分钟、第 30 分钟 ⏰
# 即第 35 分钟、第 40 分钟、第 45 分钟、第 50 分钟、第 55 分钟 ⏰
*/5 * * * * /usr/bin/python /home/pi/Desktop/five.py >> /home/pi/Desktop/five.log.txt 2>&1
# >> 追加 log, > 覆盖 log
*/5 * * * * /usr/bin/python /home/pi/Desktop/five.py > /home/pi/Desktop/five.log.txt 2>&1

# 非标准 cron ⚠️, 不通用 bug ❌ 0/5
0/5 * * * * /usr/bin/python /home/pi/Desktop/five.py >> /home/pi/Desktop/five.log.txt 2>&1

# https://github.com/vixie/cron/issues/13

# 标准 cron ✅, 通用 🚀 */5
*/5 * * * * /usr/bin/python /home/pi/Desktop/five.py >> /home/pi/Desktop/five.log.txt 2>&1

# crontab 当前登录用户,无需指定用户名 ✅
# 指定多个值的列表,逗号`,` 分割
# 即第 5 分钟、第 15 分钟、第 25 分钟、第 35 分钟、第 45 分钟、第 55 分钟 ⏰
5,15,25,35,45,55 * * * * /usr/bin/python /home/pi/Desktop/five.py >> /home/pi/Desktop/five.log.txt 2>&1
# >> 追加 log, > 覆盖 log
5,15,25,35,45,55 * * * * /usr/bin/python /home/pi/Desktop/five.py > /home/pi/Desktop/five.log.txt 2>&1
# crontab 当前登录用户,无需指定用户名 ✅
# 指定连续值的范围,连字符`-` 分割
# 即第 5 分钟~第 55 分钟 ⏰
5-55 * * * * /usr/bin/python /home/pi/Desktop/five.py >> /home/pi/Desktop/five.log.txt 2>&1
# >> 追加 log, > 覆盖 log
5-55 * * * * /usr/bin/python /home/pi/Desktop/five.py > /home/pi/Desktop/five.log.txt 2>&1
# /etc/crontab 系统 crontab, 指定用户名 ✅ pi

# 每一个小时的第 5 分钟执行一次,即一天 24 小时会执行 24 次 ⏰
5 * * * * pi /usr/bin/python /home/pi/Desktop/gpio-system.py 3 >> /home/pi/Desktop/gpio-system.log.txt 2>&1
# >> 追加 log, > 覆盖 log
5 * * * * pi /usr/bin/python /home/pi/Desktop/gpio-system.py 3 > /home/pi/Desktop/gpio-system.log.txt 2>&1

GitHub Actions

Scheduled events

# Cron syntax has five fields separated by a space, and each field represents a unit of time.

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of the month (1 - 31)
│ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
│ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
│ │ │ │ │                                   
│ │ │ │ │
│ │ │ │ │
* * * * *

https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#schedule

on:
  schedule:
    # * is a special character in YAML so you have to quote this string
    # This example triggers the workflow every 15 minutes
    - cron:  '*/15 * * * *'

https://github.com/xgqfrms/gitHub-secrets-all-in-one/blob/main/.github/workflows/auto_commit.yml

on:
  schedule:
    # * is a special character in YAML so you have to quote this string
    # This example triggers the workflow every 15 minutes
    - cron:  '8 8 * * *'

https://github.com/learning-js-by-reading-source-codes/vanillawebprojects/blob/main/.github/workflows/schedule.yml

每晚的 21:30 重启

# 每晚的 21:30 重启smb
30 21 * * * /etc/init.d/smb restart

https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html#smb

cron logs

$ tail -f /var/log/cron
tail: /var/log/cron: No such file or directory



➜  ~ grep ./auto-install-npm.sh /var/log/cron
grep: /var/log/cron: No such file or directory

➜  ~ grep "auto-install-npm.sh" /var/log/cron
grep: /var/log/cron: No such file or directory


cron logs

https://stackoverflow.com/questions/4811738/how-to-log-cron-jobs

# 2>&1, 标准输出和错误输出二合一

* * * * * ./auto-install-npm.sh >> /var/log/auto-install-npm.log 2>&1

linux 2>&1

https://stackoverflow.com/questions/818255/in-the-shell-what-does-21-mean

demo

name: ESLint

on:
  push:
    branches: [ "master" ]
  pull_request:
    # The branches below must be a subset of the branches above
    branches: [ "master" ]
  schedule:
    # 定时任务 crontab, 每周五的 21:24 分自动执行一次 ESLint 扫描
    # Runs at 21:24 UTC on Fri. Actions schedules run at most every 5 minutes
    # https://docs.github.com/actions/reference/workflow-syntax-for-github-actions#onschedule
    - cron: '24 21 * * 5'

https://github.com/xgqfrms/Gatsby-Tutorials-All-In-One/new/master?filename=.github%2Fworkflows%2Feslint.yml&workflow_template=eslint

# Cron syntax has five fields separated by a space, and each field represents a unit of time.

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of the month (1 - 31)
│ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
│ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
│ │ │ │ │                                   
│ │ │ │ │
│ │ │ │ │
* * * * *

refs

Linux Bash shell 脚本定时器 All In One

https://www.cnblogs.com/xgqfrms/p/15384401.html

https://github.com/xgqfrms/linux/issues/15

https://docs.github.com/actions/reference/workflow-syntax-for-github-actions#onschedule

Raspberry Pi

https://www.cnblogs.com/xgqfrms/p/17306951.html#5167317



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2020-12-03 23:04  xgqfrms  阅读(181)  评论(20)    收藏  举报