PDA

View Full Version : [BASH] Running script with cron not working as expected



Mindzai
September 28th, 2008, 08:17 PM
Hi

I'm trying to write my first shell script. The purpose is to check a folder for new files, determine if they are RAR archives, if they are, check if they have already been extracted by the script, extract them if not, and finally move them to a new location.

The script I have written is as follows:


#!/bin/sh

for file in $(find /media/disk/incoming/finished/ -iregex '.*\.\(rar\|001\)')
do
echo -n "checking file ${file##*/}..."
onblacklist=`grep -c $file /media/disk/incoming/unpacking/blacklist`
if [ $onblacklist -ne 0 ]; then
echo "already unpacked"
else
echo "not unpacked yet"
echo -n "unpacking..."
unrar e -inul -o- $file
echo "done!"
echo -n "adding to blacklist..."
echo $file >> /media/disk/incoming/unpacking/blacklist
echo "done!"
fi
done
ls -rt /media/disk/incoming/unpacking | grep -v '\(^unpack_torrents.sh$\|^blacklist$\|^cron.log$\) ' | xargs -I{} mv {} /media/disk/incoming/unpacked


I have 2 questions relating to this.

1. When I run this script manually, it works as I expect, ie files are unpacked into the same directory as the script (/media/disk/incoming/unpacking/) and then moved to the specified location (/media/disk/incoming/unpacked/). However, when I run this script using cron, all files get placed into my home directory, and do not get moved (or rather, i expect they do get moved but again into the home directory in which they already reside). Is there any reason why cron is causing the script to ignore my file paths? In case it matters, here is my crontab entry:


0 * * * * /media/disk/incoming/unpacking/unpack_torrents.sh >> /media/disk/incoming/unpacking/cron.log

2. I'm sure this script probably looks pretty horrible to more advanced shell coders than myself. Can anyone suggest improvements or a better way of achieving my goal than the one I'm using here?

Any help appreciated. :)

Mindzai
September 28th, 2008, 09:07 PM
Seem to have fixed question 1 by adding a cd to the correct directory first - although i'd be interested to hear why this is necessary as I'm using absolute paths for every file/directory?

Would still to hear any suggestions for question 2 too :)

Mindzai
October 1st, 2008, 10:26 AM
BUMP

Anyone able to tell me if I am on the right lines here? Or does this implementation make people want to weep when they read it?

ghostdog74
October 1st, 2008, 12:01 PM
BUMP

Anyone able to tell me if I am on the right lines here? Or does this implementation make people want to weep when they read it?

use a while read loop. This reduce the chance of breaking when file names have spaces if using the for loop.


find /media/disk/incoming/finished/ -iregex '.*\.\(rar\|001\) | while read FILE
do
....
done


other than that, good effort.

unutbu
October 1st, 2008, 07:52 PM
From "man unrar":

"unrar e" extracts files to the current directory.

When you run a script via your personal cron, the currect directory is your $HOME directory. So even though you are using absolute file paths, the "unrar e" command is still dumping the files to $HOME.