Results 1 to 3 of 3

Thread: Shell Script with Cron - help needed

  1. #1
    Join Date
    Aug 2006
    Beans
    44

    Shell Script with Cron - help needed

    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.
    Last edited by Mindzai; September 28th, 2008 at 02:23 AM.

  2. #2
    Join Date
    Aug 2006
    Beans
    44

    Re: Shell Script with Cron - help needed

    bump - anyone able to help me out?

  3. #3
    Join Date
    Oct 2005
    Location
    @ /home
    Beans
    804
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Shell Script with Cron - help needed

    In the cron job redirect standard error to standard output, otherwise you will not be able to see if something wrong happened, i.e.:

    Code:
    0 * * * * /media/disk/incoming/unpacking/unpack_torrents.sh >> /media/disk/incoming/unpacking/cron.log  2>&1
    Run a couple of tests and see what happens

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
  •