除非通过 sh 命令调用,否则 cron 脚本不起作用



我有一个脚本保存在名为file.sh的文件中它由命令

执行
chmod +x file.sh

file.sh

#!/bin/sh
find /var/log/*.gz  -printf "%T@ %Tc %pn"|sort -n|tail -n +6|sudo xargs rm|echo"test"|wall

我正在尝试通过cron job执行它。

Crontab

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 job启动一次,我在终端中收到echo

Butxargs rm doest works。我知道为什么。

当我执行sudo sh file.sh命令时,显示正常,文件被删除。

请帮我找出脚本在cron-job中不工作的原因。真奇怪。

Thank You !

问题是你的printf声明…我很确定,您还应该在root的邮件中看到rm的错误消息,例如:1634986839.0000000000 Sun 24 Oct 2021 12:00:39 AM UTC /var/log/syslog.2.gz不存在