crontab 详细用法、定时任务 、时间规则

Crontab可以用来在系统中定期的执行任务

1、添加或更新crontab中的命令
crontab -e

2、查看当前系统登录用户的Crontab命令集合
crontab -l

Ubuntu系统使用crontab定时执行python脚本

crontab -e配置完成后,如何把保存并退出?

1.Ctrl+O 写入

2.出现“FIile name to Write…”,输入Enter

3.Ctrl+x 保存输出

启动crontab service cron start
查看crontab service cron status

使用实例

1、每天02:00执行任务

  0 2 * * *  /bin/sh/cleanCache.sh

2、每天5:00和17:00执行任务

0 5,17 * * *  /bin/sh/cleanCache.sh

3、每分钟执行一次任务

  * * * * *  /bin/sh/cleanCache.sh

4、每周日 17:00 执行任务

0 17 * * sun  /bin/sh/cleanCache.sh

5、每 10min 执行一次任务

 */10 * * * *  /bin/sh/cleanCache.sh

6、在特定的某几个月执行任务

* * * jan,may,aug *  /bin/sh/cleanCache.sh

7、在每周五、周日的17点执行任务

0 17 * * sun,fri  /bin/sh/cleanCache.sh

8、在某个月的第一个周日执行任务

0 2 * * sun  [ $(date +%d) -le 07 ]  /bin/sh/cleanCache.sh

9、每四个小时执行一个任务

0 */4 * * *   /bin/sh/cleanCache.sh

10、每周一、周日执行任务

0 4,17 * * sun,mon /bin/sh/cleanCache.sh

11、每个30秒执行一次任务
我们没有办法直接通过上诉类似的例子去执行,因为最小的是1min。但是我们可以通过如下的方法。

  * * * * * /scripts/script.sh
  * * * * *  sleep 30; /scripts/script.sh

12、多个任务在一条命令中配置

   * * * * *  /bin/sh/cleanCache1.sh;/bin/sh/cleanCache2.sh

13、每年执行一次任务

  @yearly  /bin/sh/cleanCache.sh

14、系统重启时执行

  @reboot   /bin/sh/cleanCache.sh

linux服务器定时清除缓存buff/cache脚本

在这里插入图片描述
echo “开始清除缓存”
sync;sync;sync #写入硬盘,防止数据丢失
sleep 10 #延迟10秒
echo 3 > /proc/sys/vm/drop_caches

另外:手动清除缓存的命令

sudo sh -c “echo 1 > /proc/sys/vm/drop_caches”

sudo sh -c “echo 2 > /proc/sys/vm/drop_caches”

sudo sh -c “echo 3 > /proc/sys/vm/drop_caches”

参考https://blog.csdn.net/u013967628/article/details/83504839


版权声明:本文为qq_43919485原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。