宝塔搭建go-cron
地址:https://github.com/ouqiang/gocron
架构说明
gocron 分为两个组件:
| 组件 | 作用 | 默认端口 |
|---|---|---|
| gocron | Web 管理端 / 调度器 | 5920 |
| gocron-node | 任务执行节点 | 5921 |
通常部署方式:
[浏览器] ↓[gocron 调度中心] ↓[gocron-node 执行节点] ↓[Shell / HTTP 任务]
宝塔一键安装lnmp,选择以下版本 源码编译安装
nginx 选择 Nginx 1.30.2,
php 选择 PHP 8.3.30,
mysql 选择 MySQL 8.0.45
[root@localhost ~]# nginx -v nginx version: nginx/1.30.2[root@localhost ~]# mysql -V mysql Ver 8.0.45 for Linux on x86_64 (Source distribution)[root@localhost ~]# php -v PHP 8.3.30 (cli) (built: May 23 2026 15:33:59) (NTS)Copyright (c) The PHP Group Zend Engine v4.3.30, Copyright (c) Zend Technologies
[root@localhost ~]# php --ini Configuration File (php.ini) Path: /www/server/php/83/etc Loaded Configuration File: /www/server/php/83/etc/php-cli.ini Scan for additional .ini files in: (none)Additional .ini files parsed: (none)
[root@localhost ~]# which php/usr/bin/php
php -i | grep "Configure Command"
你会看到编译时的安装路径:--prefix=/usr/local/php
--prefix= 后面就是 PHP 安装根目录
[root@localhost ~]# php -i | grep "Configure Command"Configure Command => './configure' '--prefix=/www/server/php/83' '--with-config-file-path=/www/server/php/83/etc' '--enable-fpm' '--with-fpm-user=www' '--with-fpm-group=www' '--enable-mysqlnd' '--with-mysqli=mysqlnd' '--with-pdo-mysql=mysqlnd' '--with-iconv-dir' '--with-freetype' '--with-jpeg' '--with-zlib' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-bcmath' '--enable-shmop' '--enable-sysvsem' '--enable-inline-optimization' '--with-curl' '--enable-mbregex' '--enable-mbstring' '--enable-intl' '--enable-ftp' '--enable-gd' '--with-openssl' '--with-mhash' '--enable-pcntl' '--enable-sockets' '--with-xmlrpc' '--enable-soap' '--with-gettext' '--disable-fileinfo' '--enable-opcache' '--with-sodium=/usr/local/libsodium' '--with-webp'
[root@localhost ~]# cd /usr/local/[root@localhost local]# wget https://github.com/ouqiang/gocron/releases/download/v1.5.3/gocron-v1.5.3-linux-amd64.tar.gz[root@localhost local]# wget https://github.com/ouqiang/gocron/releases/download/v1.5.3/gocron-node-v1.5.3-linux-amd64.tar.gz
[root@localhost local]# tar -zxvf gocron-v1.5.3-linux-amd64.tar.gz[root@localhost local]# tar -zxvf gocron-node-v1.5.3-linux-amd64.tar.gz[root@localhost local]# mv gocron-linux-amd64/ gocron[root@localhost local]# mv gocron-node-linux-amd64/ gocron-node[root@localhost local]# chmod +x gocron/[root@localhost local]# chmod +x gocron-node/
[root@localhost gocron]# ./gocron -v gocron version Version: v1.5.3Go Version: go1.13Git Commit: 93c544f Built: 2020-02-20 21:23:25 OS/ARCH: linux/amd64
[root@localhost local]# cd gocron[root@localhost gocron]# ./gocron -v
[root@localhost gocron-node]# cd /usr/local/gocron-node/[root@localhost gocron-node]# ./gocron-node
[root@localhost gocron-node]# lsof -i:5920
宝塔面板自带的 进程守护管理器,它可以帮你把 gocron 做成常驻后台进程,崩溃自动重启、开机自启,比 nohup 更稳定。
先确认 gocron 已经能正常运行:
1.找到你的 gocron 程序目录,比如:
/usr/local/gocron
2.确认可执行文件路径:
/usr/local/gocron/gocron
3.测试手动运行:
/usr/local/gocron/gocron
能正常启动,没有报错再继续配置。
打开进程守护管理器
在宝塔面板左侧菜单找到「软件商店」→「进程守护管理器」,点击进入 进程守护管理器。(未安装需要在 官方应用 搜索 “进程守护管理器”)
添加守护进程
点击 设置 → 「添加守护进程」,按以下格式填写:
| 配置项**** | 填写内容**** | 说明**** |
| 进程名称 | gocron-web | 自定义,方便识别 |
| 启动用户 | root | 或你服务器的管理员用户 |
| 启动目录 | /usr/local/gocron | 你的 gocron 程序所在目录 |
| 启动命令 | ./gocron web | 或完整路径 /usr/local/gocron/gocron web |
| 进程数 | 1 | 只需要运行一个实例 |
| 启动优先级 | 999 | 默认999 |
| 进程目录 | /usr/local/gocron |
同理,配置 gocron-node 守护进程。需要注意的是 gocron-node 的守护进程的 启动用户不能选择 root。
配置完两个守护进程后,点击启动,开启服务。如果 进程状态非 “RUNNING”,查看对应 “日志-服务日志”排查问题。


在宝塔面板左侧菜单找到「网站」→「添加站点」,
新增一个域名 比如:ck.cron.com,根目录选择项目目录,比如 : /www/wwwroot/ck.cron.com, 数据库选择不创建,php版本选择 纯静态。
添加完站点后,点击 该站点 《设置》,选择配置文件,添加如下配置:做一个反向代理的配置,外部解析域名 ck.cron.com
upstream cron {
server 127.0.0.1:5920;
keepalive 10240;}server {
server_name ck.cron.com;
listen 80;
access_log /www/wwwlogs/ck.cron.com.log;
root /www/wwwroot/ck.cron.com;
location ^~ / {
proxy_set_header X-Real-IP $http_ali_cdn_real_ip;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Connection "";
if (!-f $request_filename){
proxy_pass http://cron;
}
}}1.访问 新配置的域名 http://ckcron.com,( 或 http://服务器IP:5920),如果能正常打开 gocron 后台,说明程序运行正常。
1.访问该 域名,会跳转到安装配置页面,填写数据库 信息(数据库要 提前新建好),和后台管理员登录信息,稍等片刻,即会在 该数据库新增4个表:
kfmcs_setting kfmcs_task kfmcs_task_host kfmcs_task_log
go-cron如何使用呢
一、先搞懂核心概念:任务中心 vs 执行节点
你现在打开的 ck.cron.com 是任务中心(管理端),它负责:
管理定时任务
记录日志
下发任务指令
但任务要在服务器上执行,必须有执行节点(gocron-node)。
简单来说:
没有节点 → 任务无法运行
节点越多 → 任务可以在多台服务器上分布式执行
二、如果你只在当前这台服务器跑任务(单节点)
你只需要在这台服务器上安装并启动 gocron-node,和任务中心关联起来即可。
1.登录 ck.cron.com
2.进入「任务节点」→「添加节点」
节点名称:自定义,常见项目名( 或比如 “本机节点”)
节点地址:你的服务器 IP(或 127.0.0.1)
节点端口:5921(默认)
3.保存后,节点状态会变成「在线」,说明关联成功。
4.新增的任务节点添加完成后,点击 《任务节点》列表里面的。《测试链接》,提示链接成功, 则配置成功。

1.进入任务管理,「定时任务」→「新增」
填写关键信息:
任务名称:测试任务
执行方式:Shell
定时规则:* */1 * * *(每分钟执行一次)
任务节点:选择你刚才添加的节点
执行命令:
Ls
2.保存并启动任务
3.验证是否成功
几分钟后,查看日志:
能看到 输出 gocron-node 记录,说明任务执行成功了。
杀掉进程测试,是否可以自动重启
2.配置进程守护后,杀掉进程测试自动重启:
[root@localhost gocron]# ps -ef | grep gocron root 911760 911307 0 10:55 ? 00:00:00 /usr/local/gocron/gocron web www 912549 911307 0 11:27 ? 00:00:00 /usr/local/gocron-node/gocron-node root 913782 909720 0 14:30 pts/2 00:00:00 grep --color=auto gocron[root@localhost gocron]# pkill gocron[root@localhost gocron]# ps -ef | grep gocron # 几秒后进程会被自动拉起 root 913800 911307 1 14:31 ? 00:00:00 /usr/local/gocron/gocron web www 913812 911307 0 14:31 ? 00:00:00 /usr/local/gocron-node/gocron-node root 913819 909720 0 14:31 pts/2 00:00:00 grep --color=auto gocron
问题1:could not be opened in append mode: FaiLed to open stream: Permission denied
原因:gocron-node 运行用户没有写权限

参考:
本文为崔凯原创文章,转载无需和我联系,但请注明来自冷暖自知一抹茶ckhttp://www.cksite.cn