docker alpine里的crond


目录
  1. 1. 问题发现
  2. 2. 解决方法
  3. 3. 注意事项

crond是linux系统运行定时任务的程序。发现docker里定时任务设置了,但并没有执行。本文记录了处理过程。

问题发现

原来用github actions定时执行的脚本由于改成vercel部署,域名指向了vercel,github.io不能直接访问,不想再设二级域名,就放到玩客云中的docker运行。由于alpine没有安装service,crontab没有正确执行。

解决方法

前台运行crond,由于没有服务,要把crond加到docker入口run.sh里。
原来的run.sh:

1
2
3
4
5
#!/bin/sh
# 后台启动
php-fpm -D
# 关闭后台启动,hold住进程
nginx -g 'daemon off;'

想也没想,直接把crond加到最后一句。重启docker,再进入docker,发现进程中没有crond。手动执行crond就有。
最后才发现是nginx这一句的问题,把crond加到前一句就正常了。

1
2
3
4
5
6
#!/bin/sh
# 后台启动
php-fpm -D
crond
# 关闭后台启动,hold住进程
nginx -g 'daemon off;'

ps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
PID   USER     TIME  COMMAND
1 root 0:00 {run.sh} /bin/sh /run.sh
8 root 0:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
9 www-data 0:00 php-fpm: pool www
10 www-data 0:00 php-fpm: pool www
12 root 0:00 crond
13 root 0:00 nginx: master process nginx -g daemon off;
14 nginx 0:00 nginx: worker process
15 nginx 0:00 nginx: worker process
16 nginx 0:00 nginx: worker process
17 nginx 0:00 nginx: worker process
18 root 0:00 [run-parts]
19 root 0:00 sh
26 root 0:00 ps

注意事项

crontab里的命令和文件要用绝对路径,比如.py文件和生成的.html文件,否则不会生成文件。

问题解决。
毛病出在alpine和惯用的debian系不一样,docker里的更精简,差异太大了。