【发布时间】:2021-12-10 10:48:47
【问题描述】:
我有一个脚本保存在名为 file.sh 的文件中 通过命令执行
chmod +x file.sh
文件.sh
#!/bin/sh
find /var/log/*.gz -printf "%T@ %Tc %p\n"|sort -n|tail -n +6|sudo xargs rm|echo"test"|wall
我正在尝试通过 cron 作业执行它。
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 作业每分钟启动一次脚本,我在终端收到回声。
但是 xargs rm 不起作用。我知道为什么。
当我执行命令 sudo sh file.sh 时,正常,文件被删除。
请帮我找出脚本在 cron-job 中不起作用的原因。 很奇怪。
谢谢!
【问题讨论】: