Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
2 Now edited to include full script and crontab included. Thanks.
source | link

I have a bash script which loops through a list of filenames in a text file, deduces a date from the filename and then calls a library script (./dropbox_uploader.sh delete [filname]) to conditionally delete a remote file in my Dropbox account using Dropbox's api. The condition is simply whether the file is older than [n] days. n is passed into the script in position $2. $1 is a text file containing the entries.

Extract from myMy script:

#!/bin/bash
threshold=$(opendate file-d and"$2 loopdays aago" list+%s)
go=0
stay=0
rm of/home/pi/Dropbox-Uploader/camDeleteLog.txt
echo filenames)"deleting older than $2 
 days ago ..."
...
while IFS='' read -r line || [[-n "$line" ]]; do
        line=$(echo $line | xargs)      # trim spaces from the filename, just i$
        y=${line:0:4}
        m=${line:5:2}
        d=${line:8:2}
        #echo "Filedate is $y-$m-$d"
        seconds=$(date -d "$y-$m-$d" +%s)

        if ((seconds < threshold))
        then
                echo "Deleting file $go, $line"
                ((go++))
                ###echo "./dropbox_uploader.sh delete \"$line\""
               ./dropbox_uploader.sh delete "$line" >> /home/pi/Dropbox-Uploader/camDeleteLog.txtUpload$

        else
                echo "$line is too new to delete"
                ((stay++))
        fi
    done < "$1"
    echo "The dropbox files were purged: $go files deleted and $stay were kept." | mail$

My crontab for user pi:

0 */8 * * * /home/pi/Dropbox-sUploader/moveVideostoDropbox.py "Cam# filesJOB_ID_1
0 have0,6,12,18 been* purged"* myname@mydomain* Dropbox-Uploader/processDBFiles.uksh # JOB_ID_2
0 1,7,13,19 * * * Dropbox-Uploader/processByDate.sh camfiles.txt 2 # JOB_ID_3

Job 3 is the one that does the date processing and deleting. Jobs 2 and 3 were originally run together but I suspected (wrongly) that this was the root of my problem so split the 2 and ran them an hour apart as an experiement - Job 2 will never take more than 1 hour to run.

I have a bash script which loops through a list of filenames in a text file, deduces a date from the filename and then calls a library script (./dropbox_uploader.sh delete [filname]) to conditionally delete a remote file in my Dropbox account using Dropbox's api. The condition is simply whether the file is older than [n] days.

Extract from my script:

(open file and loop a list of filenames)    
 ...
...
    if ((seconds < threshold))
        then
            echo "Deleting file $go, $line"
            ((go++))
            ./dropbox_uploader.sh delete "$line" >> /home/pi/Dropbox-Uploader/camDeleteLog.txt

        else
            echo "$line is too new to delete"
            ((stay++))
        fi
    done < "$1"
    echo "The dropbox files were purged: $go files deleted and $stay were kept." | mail -s "Cam files have been purged" myname@mydomain.uk

I have a bash script which loops through a list of filenames in a text file, deduces a date from the filename and then calls a library script (./dropbox_uploader.sh delete [filname]) to conditionally delete a remote file in my Dropbox account using Dropbox's api. The condition is simply whether the file is older than [n] days. n is passed into the script in position $2. $1 is a text file containing the entries.

My script:

#!/bin/bash
threshold=$(date -d "$2 days ago" +%s)
go=0
stay=0
rm /home/pi/Dropbox-Uploader/camDeleteLog.txt
echo "deleting older than $2 days ago ..."

while IFS='' read -r line || [[-n "$line" ]]; do
        line=$(echo $line | xargs)      # trim spaces from the filename, just i$
        y=${line:0:4}
        m=${line:5:2}
        d=${line:8:2}
        #echo "Filedate is $y-$m-$d"
        seconds=$(date -d "$y-$m-$d" +%s)

        if ((seconds < threshold))
        then
                echo "Deleting file $go, $line"
                ((go++))
                ###echo "./dropbox_uploader.sh delete \"$line\""
               ./dropbox_uploader.sh delete "$line" >> /home/pi/Dropbox-Upload$

        else
                echo "$line is too new to delete"
                ((stay++))
        fi
done < "$1"
echo "The dropbox files were purged: $go files deleted and $stay were kept." | $

My crontab for user pi:

0 */8 * * * /home/pi/Dropbox-Uploader/moveVideostoDropbox.py # JOB_ID_1
0 0,6,12,18 * * * Dropbox-Uploader/processDBFiles.sh # JOB_ID_2
0 1,7,13,19 * * * Dropbox-Uploader/processByDate.sh camfiles.txt 2 # JOB_ID_3

Job 3 is the one that does the date processing and deleting. Jobs 2 and 3 were originally run together but I suspected (wrongly) that this was the root of my problem so split the 2 and ran them an hour apart as an experiement - Job 2 will never take more than 1 hour to run.

1
source | link

Local variables in Bash script to be run by Cron

I have a bash script which loops through a list of filenames in a text file, deduces a date from the filename and then calls a library script (./dropbox_uploader.sh delete [filname]) to conditionally delete a remote file in my Dropbox account using Dropbox's api. The condition is simply whether the file is older than [n] days.

2 local variables, $go and $stay count the instances of where a file is eligible for deletion, or not. At the end of my script, I fire off an email via the mail program using the values of the 2 variables..

My problem is that if I run this from the command line everything works just fine and the email reads:

"The dropbox files were purged: 554 files deleted and 310 were kept."

However, if the script is run via Cron using my user's crontab then it seems that the local variables are both zero / null, and the email is:

The dropbox files were purged: 0 files deleted and 0 were kept.

I can run the script again manually after the cron run and the variables are non zero.

I think I'm missing something fundamental about the way cron runs for the user and would be grateful for suggestions.

Extract from my script:

(open file and loop a list of filenames)    
...
...
    if ((seconds < threshold))
        then
            echo "Deleting file $go, $line"
            ((go++))
            ./dropbox_uploader.sh delete "$line" >> /home/pi/Dropbox-Uploader/camDeleteLog.txt

        else
            echo "$line is too new to delete"
            ((stay++))
        fi
    done < "$1"
    echo "The dropbox files were purged: $go files deleted and $stay were kept." | mail -s "Cam files have been purged" myname@mydomain.uk