crontab 在调用包含输出的 .sh 时不输出任何内容
crontab output nothing when calling a .sh that contains an output
我在 /home/me
文件夹中有以下 test.sh
文件
#!/bin/sh
_now=$(date +"%Y_%m_%d")
_file="/home/me/$_now.txt"
speedtest-cli --simple > $_file
其中 speedtest-cli
是一个 python 脚本,它提供互联网和 dll 速度信息:https://github.com/sivel/speedtest-cli。
从 /home/me
调用 test.sh
效果很好:我的 yyy_mm_dd.txt
输出包含所有信息(dll 加速等)。
但是当我尝试从 crontab 调用 test.sh
时,我得到一个空的 yyy_mm_dd.txt
文件(里面什么都没有)。
里面crontab-e
20 20 * * * /home/me/test.sh
我是不是做错了什么?
对于交互式 shell,您的 PATH
可能与 cronjob 运行的上下文不同,因此您应该在 crontab 条目中指定 speedtest-cli
的完整路径。
我怀疑是路径问题,所以
任选其一:
- 在脚本顶部添加
PATH=/usr/local/bin:/bin:/usr/bin
- 在
crontab -e
的顶部添加:PATH=/usr/local/bin:/bin:/usr/bin
在他自己的行
source ~/.bashrc
在脚本的顶部
- 为脚本中的每个命令添加完整路径
我在 /home/me
文件夹中有以下 test.sh
文件
#!/bin/sh
_now=$(date +"%Y_%m_%d")
_file="/home/me/$_now.txt"
speedtest-cli --simple > $_file
其中 speedtest-cli
是一个 python 脚本,它提供互联网和 dll 速度信息:https://github.com/sivel/speedtest-cli。
从 /home/me
调用 test.sh
效果很好:我的 yyy_mm_dd.txt
输出包含所有信息(dll 加速等)。
但是当我尝试从 crontab 调用 test.sh
时,我得到一个空的 yyy_mm_dd.txt
文件(里面什么都没有)。
里面crontab-e
20 20 * * * /home/me/test.sh
我是不是做错了什么?
对于交互式 shell,您的 PATH
可能与 cronjob 运行的上下文不同,因此您应该在 crontab 条目中指定 speedtest-cli
的完整路径。
我怀疑是路径问题,所以
任选其一:
- 在脚本顶部添加
PATH=/usr/local/bin:/bin:/usr/bin
- 在
crontab -e
的顶部添加:PATH=/usr/local/bin:/bin:/usr/bin
在他自己的行 source ~/.bashrc
在脚本的顶部- 为脚本中的每个命令添加完整路径