除非由 sh 命令调用,否则 Cron 脚本不起作用
Cron script doesn't work unless called by sh command
我有一个脚本保存在名为 file.sh 的文件中
它由命令执行
chmod +x file.sh
file.sh
#!/bin/sh
find /var/log/*.gz -printf "%T@ %Tc %p\n"|sort -n|tail -n +6|sudo xargs rm|echo"test"|wall
我正在尝试通过 cron 作业来执行它。
定时任务
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
* * * * * root /etc/file.sh
cron 作业每分钟启动一次脚本,我在终端中得到回显。
但是 xargs rm 确实有效。我知道为什么。
当我执行命令sudo sh file.sh时,OK,文件被删除。
请帮我找出脚本在 cron-job 中不工作的原因。
很奇怪。
谢谢!
问题出在你的 printf
语句上......我相当确定你也应该在 root 的邮件中看到来自 rm
的错误消息,大意如下:
1634986839.0000000000 Sun 24 Oct 2021 12:00:39 AM UTC /var/log/syslog.2.gz
不存在。
我有一个脚本保存在名为 file.sh 的文件中 它由命令执行
chmod +x file.sh
file.sh
#!/bin/sh
find /var/log/*.gz -printf "%T@ %Tc %p\n"|sort -n|tail -n +6|sudo xargs rm|echo"test"|wall
我正在尝试通过 cron 作业来执行它。
定时任务
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
* * * * * root /etc/file.sh
cron 作业每分钟启动一次脚本,我在终端中得到回显。
但是 xargs rm 确实有效。我知道为什么。
当我执行命令sudo sh file.sh时,OK,文件被删除。
请帮我找出脚本在 cron-job 中不工作的原因。 很奇怪。
谢谢!
问题出在你的 printf
语句上......我相当确定你也应该在 root 的邮件中看到来自 rm
的错误消息,大意如下:
1634986839.0000000000 Sun 24 Oct 2021 12:00:39 AM UTC /var/log/syslog.2.gz
不存在。