1

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.

The problem I am heaving is that if I run the script by hand ./cronJob.sh it works as it should. But when I put it on a crontab list it does not delete directories, but only outputs two lines in log.

#!/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

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

Log output

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I added this line as well and scheduled the job to run a minute from now. Thank you
1

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

And check file permission and owner of the file

3 Comments

I edited the crontab job. I will post if it worked on monday. Thank you for fast response.
Unfortunatelly I do not know how to test a cronjob before it is scheduled.
schedule it two minutes after from now.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.