# scheduler **Repository Path**: zhangxi9132/scheduler ## Basic Information - **Project Name**: scheduler - **Description**: zhangxi9132/scheduler - **Primary Language**: C++ - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 2 - **Created**: 2023-06-27 - **Last Updated**: 2024-04-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # scheduler #### 介绍 scheduler 实现了一个类似于 crontab 的定时任务功能, 功能如下 ```c++ // min hour day_of_month month day_of_year day_of_week week_of_month week_day_of_month mon_day_of_year week_day_of_year // 05 23 * * * 1,2,3 * * * 1:1,-1:7 // day_of_week(1,2,3) 与 week_day_of_year(1:1,-1:7) 冲突就会加入失败, return false; bool AddTermTask(const char *fmt, TimerCallback cb, const char * = "* * * * * * * * * *"); ``` ```shell // ----------------------------------- // crontab 示例, // 第1列分钟1~59 // 第2列小时1~23(0表示子夜) // 第3列日1~31 // 第4列月1~12 // 第5列星期0~6(0表示星期天) // 第6列要运行的命令 // 1. // 59 23 * * * /home/oracle/scripts/alert_log_archive.sh >/dev/null 2>&1 // 表示每天23点59分执行脚本/home/oracle/scripts/alert_log_archive.sh // 2. // */5 * * * * /home/oracle/scripts/monitoring_alert_log.sh >/dev/null 2>&1 // 表示每5分钟执行一次脚本/home/oracle/scripts/monitoring_alert_log.sh // 3. // 0 20 * * 1-5 mail -s "**********" kerry@domain.name < /tmp/maildata // 周一到周五每天下午 20:00 寄一封信给 kerry@domain.name // // ----------------------------------- // // mgr_scheduler 定时任务示例, // 1. 同样的, * 表述无效参数, // min hour day_of_month month day_of_year day_of_week week_of_month week_day_of_month mon_day_of_year week_day_of_year // 05 23 12 * * * * * * * // 每月12号的的 23:05 做任务, // 2. // min hour day_of_month month day_of_year day_of_week week_of_month week_day_of_month mon_day_of_year week_day_of_year // 05 23 * 1-9 * 1,3,5 * * * * // 1-9月的 Monday Wednesday Friday 的 23:05 做任务, // 3. // min hour day_of_month month day_of_year day_of_week week_of_month week_day_of_month mon_day_of_year week_day_of_year // 05 23 * * * * * 3:1,2:3 * * // 每个月第3周的的Monday 和第2周的Wednesday 执行任务, // 4. // min hour day_of_month month day_of_year day_of_week week_of_month week_day_of_month mon_day_of_year week_day_of_year // 05 23 1,-1 * * * * * * * // 每个月第一天或者最后一天 执行任务, // 5. // min hour day_of_month month day_of_year day_of_week week_of_month week_day_of_month mon_day_of_year week_day_of_year // 05 23 * * * * * * 3:1,5:1 * // 每年的 3.1 或者 5.1 执行任务, // 6. // min hour day_of_month month day_of_year day_of_week week_of_month week_day_of_month mon_day_of_year week_day_of_year // 05 23 * * * * * * * 1:1,-1:7 // 每年的 第一周的第一天 和 最后一周的的第7天 执行任务, // // ----------------------------------- ``` 参考 Bacula 实现。 Bacula 中,初始阶段,压入任务时候,当距离任务触发不足60s,不会触发,这里同样也是, 如 14:59:32 启动的 scheduler,设置的每天 15:00:00 启动一个任务,今天15点的任务并不会执行,明天及以后的任务会被执行。 使用: ```c++ auto f = std::bind(printANum, 100); auto f2 = std::bind(printAString, "hahahahhaha"); Scheduler scheduler(&timerevent, seqId); scheduler.AddTermTask("43 15 * * * 1,2,3,4,5, * * * *", f); scheduler.AddTermTask("43 15 * * * 1,2,3,4,5, * * * *", f2); scheduler.StartTerm(); scheduler.AddTermTask("42 15 28,4 * * * * * * *", f); scheduler.AddTermTask("42 15 28,4 * * * * * * *", f2); ``` #### 软件架构 软件架构说明 #### 安装教程 1. xxxx 2. xxxx 3. xxxx #### 使用说明 1. xxxx 2. xxxx 3. xxxx #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)