crontab的使用

crontab的使用

我们常用的命令如下:

crontab 
    -e      (编辑工作表)
    -l      (列出工作表里的命令)
    -r      (删除工作作)
*/1 * * * *  python3 test_ape.py >> test.log 2&1&
分、时、日、月、周


命令: service crond start 开启服务
命令: service crond stop 关闭服务
命令: service crond restart 重启服务
命令: service crond reload 重新载入配置

实例

实例0:每10秒执行一次myCommand

*/1 * * * * python3 test_ape.py >> test.log 2&1&

test_ape.py

import datetime
import time

import redis

pool_100_7 = redis.ConnectionPool(host='10.0.3.100', port=6379, db=8, password='EfcHGSzKqg6cfzWq')
rds_100_7 = redis.StrictRedis(connection_pool=pool_100_7)


def start():
    key = "jeff_test"
    a = str(datetime.datetime.now())
    rds_100_7.rpush(key,a)


def test():
    a = 0
    while True:
        start()
        time.sleep(10)  # 每隔10秒执行一次
        a += 10
        if a > 55:
            break

if __name__ == '__main__':
    test()

实例1:每1分钟执行一次myCommand

* * * * * myCommand
*/1 * * * * myCommand

实例2:每小时的第3和第15分钟执行

3,15 * * * * myCommand

实例3:在上午8点到11点的第3和第15分钟执行

3,15 8-11 * * * myCommand

实例4:每隔两天的上午8点到11点的第3和第15分钟执行

3,15 8-11 */2  *  * myCommand

实例5:每周一上午8点到11点的第3和第15分钟执行

3,15 8-11 * * 1 myCommand

实例6:每晚的21:30重启smb

30 21 * * * /etc/init.d/smb restart

实例7:每月1、10、22日的4 : 45重启smb

45 4 1,10,22 * * /etc/init.d/smb restart

实例8:每周六、周日的1 : 10重启smb

10 1 * * 6,0 /etc/init.d/smb restart

实例9:每天18 : 00至23 : 00之间每隔30分钟重启smb

0,30 18-23 * * * /etc/init.d/smb restart

实例10:每星期六的晚上11 : 00 pm重启smb

0 23 * * 6 /etc/init.d/smb restart

实例11:每一小时重启smb

0 */1 * * * /etc/init.d/smb restart

实例12:晚上11点到早上7点之间,每隔一小时重启smb

0 23-7/1 * * * /etc/init.d/smb restart
posted @ 2021-04-25 16:20  Jeff的技术栈  阅读(68)  评论(0编辑  收藏  举报
回顶部