我在 docker 里面起了两个定时任务 /var/spool/cron/crontabs/root
* * * * * python3 /reminder/webhook/main.py * * * * * echo "1" >> /reminder/webhook/1.txt
文件权限如下
root@c565c6a20cba:/reminder/webhook# ls -la total 36 drwxr-xr-x 4 root root 4096 Oct 24 09:19 . drwxr-xr-x 3 root root 4096 Oct 24 08:19 .. -rw-r--r-- 1 root root 22 Oct 24 09:29 1.txt drwxr-xr-x 2 root root 4096 Oct 24 08:22 __pycache__ -rw-r--r-- 1 root root 434 Oct 24 08:19 configs.py drwxr-xr-x 2 root root 4096 Oct 24 08:19 data -rw-r--r-- 1 root root 1329 Oct 24 08:19 main.py -rw-r--r-- 1 root root 2866 Oct 24 08:19 monitor.py -rwxr--r-- 1 root root 16 Oct 24 08:23 run.sh
为什么第一个定时任务不起作用呢
![]() | 1 goodryb 2022-10-24 17:41:31 +08:00 |
2 fantasticlw 2022-10-24 17:50:26 +08:00 也许是因为 cron 从非交互式、非登录 shell 运行作业,所以当 cron 执行作业时,它不会从~/.bashrc 、~/.bash_profile 、/etc/profile 等文件加载环境变量。所以对于 cron 不知道 PATH 环境变量,自然不知道 python3 的位置 |
![]() | 3 Vegetable 2022-10-24 18:15:26 +08:00 你就当 cron 没有 PATH ,所以 python 他是找不到的。 |
![]() | 4 ClericPy 2022-10-24 18:39:11 +08:00 via Android cron 启动时候环境变量啥的基本都是空的... 吃过亏,你重定向 stderr 看看就知道了 |
![]() | 5 GlobalNPC 2022-10-24 18:49:28 +08:00 |
![]() | 6 oldshensheep 2022-10-24 18:50:34 +08:00 |
![]() | 7 yangg 2022-10-24 20:12:20 +08:00 via iPhone cronitor 是个好东西 |
![]() | 8 balabalaguguji 2022-10-24 20:23:14 +08:00 cron 是个服务,你要启动它 |
![]() | 9 liuzhaowei55 2022-10-24 21:02:12 +08:00 1. cron 前台运行,不能后台运行 2. 程序路径使用绝对路径定义 之前折腾的时候遇到过类似问题 |
10 ungrown 2022-10-24 23:44:02 +08:00 @fantasticlw #2 自信点,把也许去掉,就是 PATH 的原因。你回想一下常见发行版 Linux 中的 cron 配置文件,开头部分是不是都在添 PATH ? |
![]() | 11 proxytoworld OP |
![]() | 12 proxytoworld OP @balabalaguguji 起了 service cron start |
13 ungrown 2022-10-25 00:27:36 +08:00 @proxytoworld #11 可以的呀,你都指定绝对路径了,PATH 缺了也无所谓了 |