cronjob|Web版之gocron配置过程

对于Linux系统管理来说,配置计划任务是常有的事儿,基本直接cronjob走起(linux定时任务之crontab),但是凡事有个度,一个两个还成,如果加一堆就有一点儿尴尬了,一来查看管理不方便,二来不同任务的执行结果也不那么明了,三来添加都觉得费劲.
关键还有一点儿,竟然有人要求能不能五秒执行一次?乖乖,我大cronjbo执行间隔可是分钟级的,有啥事儿非要几秒钟一次?
于是网上找到了这款开源的定时任务管理系统: gocron

功能特性

  • Web界面管理定时任务
  • crontab时间表达式, 精确到秒
  • 任务执行失败可重试
  • 任务执行超时, 强制结束
  • 任务依赖配置, A任务完成后再执行B任务
  • 账户权限控制
  • 任务类型
    • shell任务

      在任务节点上执行shell命令, 支持任务同时在多个节点上运行

    • HTTP任务

      访问指定的URL地址, 由调度器直接执行, 不依赖任务节点

  • 查看任务执行结果日志
  • 任务执行结果通知, 支持邮件、Slack、Webhook

截图

流程图

任务

通知配置

支持平台

Windows、Linux、Mac OS

环境要求

MySQL

下载

releases

版本升级

安装

了解的差不多了,开工,我是Linux的操作ubuntu 18.04

lsb_release -a
LSB Version:    core-9.20170808ubuntu1-noarch:security-9.20170808ubuntu1-noarch
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:        18.04
Codename:       bionic

Mysql安装

这都2020年了,直接docker起一个吧

INNER_IP=`ifconfig eth0|awk 'NR==2{print $2}'`
docker run -dt \
--name=mysql-gocron \
--restart=always \
-p $INNER_IP:3306:3306 \
-p $INNER_IP:33060:33060 \
-v /etc/localtime:/etc/localtime:ro \
-v /etc/timezone:/etc/timezone:ro \
-v /data/mysql-data-gocron:/var/lib/mysql \
mysql/mysql-server:5.7.24

端口监听在内网IP上,调整时区,数据目录挂出来

查看密码

docker logs -f mysql-gocron 
[Entrypoint] MySQL Docker Image 5.7.24-1.1.8
[Entrypoint] No password option specified for new database.
[Entrypoint]   A random onetime password will be generated.
[Entrypoint] Initializing database
[Entrypoint] Database initialized
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
[Entrypoint] GENERATED ROOT PASSWORD: ubjIBOt@D3zQYz.4B=AjiGIzsYg

[Entrypoint] ignoring /docker-entrypoint-initdb.d/*

[Entrypoint] Server shut down
[Entrypoint] Setting root user as expired. Password will need to be changed before database can be used.

[Entrypoint] MySQL init process done. Ready for start up.

[Entrypoint] Starting MySQL 5.7.24-1.1.8

看到密码了吗?不用窃喜,这个密码是不让用的,需要先重设一下

 mysql -uroot -pubjIBOt@D3zQYz.4B=AjiGIzsYg
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.24

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> SET PASSWORD = PASSWORD('your_new_password');
Query OK, 0 rows affected, 1 warning (0.01 sec)

据说也可以这样

UPDATE mysql.user SET Password=PASSWORD('your_new_password')
           WHERE User='root'; 

gocron 服务端启动

#提前配置目录,并修改权限
mkdir /opt/gocron
chown 100.100 /opt/gocron -R

INNER_IP=`ifconfig eth0|awk 'NR==2{print $2}'`
docker run -dt \
--name gocron \
--restart=always \
-p $INNER_IP:5920:5920 \
-v /opt/gocron/conf:/app/conf \
-v /opt/gocron/log:/app/log \
ouqg/gocron

配置: /app/conf/app.ini
日志: /app/log/cron.log
注意,我把这两个目录挂载到/opt/gocron了,配置文件外挂是个好习惯.
然后就直接访问 0.0.0.0:5920了

镜像不包含gocron-node, gocron-node需要和具体业务一起构建

节点配置

启动

下载
cd /opt
wget https://github.com/ouqiang/gocron/releases/download/v1.5.3/gocron-node-v1.5.3-linux-amd64.tar.gz
tar zxf gocron-node-v1.5.3-linux-amd64.tar.gz
mv gocron-node-linux-amd64 gocron-node-v1.5.3

./gocron-node -allow-root -s gocron-node $INNER_IP:5921

管理界面配置节点

节点名称:写一个好记的名字
主机名:IP地址
端口 :5921可以保持不变
备注:看心情填写
保存

点击测试连接
提示连接成功

添加定时任务
自己摸索吧,注意crontab表达式,多了个秒哟

遇到的问题

  • Error 1130: Host '172.17.0.1' is not allowed to connect to this MySQL server
mysql> select user,host from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| healthchecker | localhost |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

mysql> update mysql.user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
  • 数据库不存在
mysql> create database gocron;
Query OK, 1 row affected (0.00 sec)
  • 数据库配置写入文件失败
chown 100 /opt/gocron -R

相关博文

About rainbird

IOS攻城狮
This entry was posted in linux, Mac and tagged , , , , , , , . Bookmark the permalink.

发表评论