简体   繁体   English

Shell脚本不能与crontab一起使用

[英]Shell script not working with crontab

I have a script that scans all the directories and subdirectories for those with "RC" in their name and delete all older then 40 days but always leave the last one even if it is older than 40 days. 我有一个脚本,扫描名称中包含“RC”的所有目录和子目录,并删除所有旧的40天,但即使它超过40天也总是留下最后一个。

The problem I am heaving is that if I run the script by hand ./cronJob.sh it works as it should. 我正在解决的问题是,如果我手动运行脚本./cronJob.sh它应该正常工作。 But when I put it on a crontab list it does not delete directories, but only outputs two lines in log. 但是当我把它放在crontab列表上时,它不会删除目录,而只会在日志中输出两行。

#!/bin/bash

datum=$(date -I)
MOUNTLOG=/var/log/softwareRC/

FIND=/bin/find;

deleteDir(){
    echo "-------- START $parent --------" >> $MOUNTLOG/$datum.log
    dname=$(/usr/bin/dirname $1)
    temp="${dname%\s.*}"
    temp=(${temp[@]})
    parent="${temp[0]}"
    dirNum="$($FIND $parent -maxdepth 1 -name *RC* -type d -print | wc -l)"
    najnovejsi="$($FIND $parent -maxdepth 1 -name *RC* -type d -print | sort | tail -n 1)"
    if [ $dirNum -gt 1 ]; then
            $FIND "$parent" -path "$najnovejsi" -prune -o -name *RC* -mtime +40 -print -exec rm -r "{}" \; >> $MOUNTLOG/$datum.log
    fi;
    echo "-------- END $parent --------" >> $MOUNTLOG/$datum.log
}

declare -i skipDir=1

while true
do
    oldest=$($FIND -type d -name *RC* -mtime +40 -printf '%T+ %p\n' | sort -r | tail -n $skipDir | head -n 1)
#       echo najstarejsi $oldest
    dironly=$(echo $oldest | cut -d' ' -f 2-)
    deleteDir "$dironly"

#       echo $skipDir $dironly
    /bin/sleep 1
    if [ "$dironly" = "$testna" ]; then
            break
    else
            testna=$(echo $oldest | cut -d' ' -f 2-)
            let "skipDir++"
    fi;
#       echo primerjava $testna
done

Crontab job Crontab工作

0 2 * * * /mnt/local/TempDrive/Software_RC/.cleanOld.sh

Log output 记录输出

[root@SambaServer softwareRC]# cat 2017-03-11.log
-------- START  --------
-------- END  --------

0 2 * * * sh /mnt/local/TempDrive/Software_RC/cleanOld.sh 0 2 * * * sh /mnt/local/TempDrive/Software_RC/cleanOld.sh

And check file permission and owner of the file 并检查文件权限和文件所有者

Add this line to your script: 将此行添加到您的脚本:

#!/bin/bash

exec > $MOUNTLOG/$datum.log 2>&1

datum=$(date -I)

If there is an error message from the shell or one of the executed commands, it will show up in the log file. 如果shell或其中一个执行的命令有错误消息,它将显示在日志文件中。

Thank you very much for your help and sorry for late reply. 非常感谢你的帮助,很抱歉迟到的回复。 I have figured out what was wrong. 我弄清楚出了什么问题。

The problem was in the below line. 问题出在下面。 I had to enter the whole path to the location where the script is ran from. 我必须输入到运行脚本的位置的整个路径。

Before: 之前:

oldest=$($FIND -type d -name *RC* -mtime +40 -printf '%T+ %p\n' | sort -r | tail -n $skipDir | head -n 1)

After: 后:

oldest=$($FIND /mnt/local/TempDrive/Software_RC -type d -name *RC* -mtime +40 -printf '%T+ %p\n' | sort -r | tail -n $skipDir | head -n 1)

This is the working code. 这是工作代码。

#!/bin/bash

datum=$(date -I)
MOUNTLOG=/var/log/softwareRC/

exec > $MOUNTLOG/$datum.log 2>&1

FIND=/bin/find;

deleteDir(){
    echo "-------- START $parent --------" >> $MOUNTLOG/$datum.log
    dname=$(/usr/bin/dirname $1)
    temp="${dname%\s.*}"
    temp=(${temp[@]})
    parent="${temp[0]}"
    dirNum="$($FIND $parent -maxdepth 1 -name *RC* -type d -print | wc -l)"
    najnovejsi="$($FIND $parent -maxdepth 1 -name *RC* -type d -print | sort | tail -n 1)"
    if [ $dirNum -gt 1 ]; then
            $FIND "$parent" -path "$najnovejsi" -prune -o -name *RC* -mtime +40 -print -exec rm -r "{}" \; >> $MOUNTLOG/$datum.log
    fi;
    echo "-------- END $parent --------" >> $MOUNTLOG/$datum.log
}

declare -i skipDir=1

while true
do
    oldest=$($FIND /mnt/local/TempDrive/Software_RC -type d -name *RC* -mtime +40 -printf '%T+ %p\n' | sort -r | tail -n $skipDir | head -n 1)
    dironly=$(echo $oldest | cut -d' ' -f 2-)
    deleteDir "$dironly"

    /bin/sleep 1
    if [ "$dironly" = "$testna" ]; then
            break
    else
            testna=$(echo $oldest | cut -d' ' -f 2-)
            let "skipDir++"
    fi;
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM