【发布时间】:2016-03-21 06:26:17
【问题描述】:
我正在尝试在 Ubuntu 平台上的 crontab 上运行 shell 脚本。我试过谷歌搜索和其他链接,但到目前为止没有任何帮助。
这是我的crontab:
*/2 * * * * sudo bash /data/html/mysite/site_cleanup.sh
这是我sh文件的内容:
#!/bin/sh
# How many days retention do we want ?
DAYS=0
# geting present day
now=$(date +"%m_%d_%Y")
# Where is the base directory
BASEDIR=/data/html/mysite
#where is the backup directory
BKPDIR=/data/html/backup
# Where is the log file
LOGFILE=$BKPDIR/log/mysite.log
# add to tar
tar -cvzf $now.tar.gz $BASEDIR
mv $now.tar.gz $BKPDIR
# REMOVE OLD FILES
echo `date` Purge Started >> $LOGFILE
find $BASEDIR -mtime +$DAYS | xargs rm
echo `date` Purge Completed >> $LOGFILE
相同的脚本从终端运行并给出所需的结果。
【问题讨论】:
-
我猜你没有名为“sudo”的用户,如果这是
/etc/crontab,请将sudo替换为root。 -
糟糕的是我没有保留任何 sudo,*/2 * * * * bash /data/html/mysite/site_cleanup.sh。谢谢保罗
-
这个 crontab 文件在哪里?我的意思是,它是在
/etc/下还是用crontab -e编辑的? -
使用 crontab -e 编辑
-
xargs rm很危险。并且以 root 身份运行脚本并不会使其更安全。请看BashFAQ 20
标签: linux bash shell ubuntu cron