Results 1 to 5 of 5

Thread: [BASH] Running script with cron not working as expected

  1. #1
    Join Date
    Aug 2006
    Beans
    44

    [BASH] Running script with cron not working as expected

    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:

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

    Code:
    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.

  2. #2
    Join Date
    Aug 2006
    Beans
    44

    Re: [BASH] Running script with cron not working as expected

    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

  3. #3
    Join Date
    Aug 2006
    Beans
    44

    Re: [BASH] Running script with cron not working as expected

    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?

  4. #4
    Join Date
    Sep 2006
    Beans
    2,914

    Re: [BASH] Running script with cron not working as expected

    Quote Originally Posted by Mindzai View Post
    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.
    Code:
    find /media/disk/incoming/finished/ -iregex '.*\.\(rar\|001\) | while read FILE
    do
     ....
    done
    other than that, good effort.

  5. #5
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: [BASH] Running script with cron not working as expected

    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.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •