Results 1 to 5 of 5

Thread: Loop the script

  1. #1
    Join Date
    Aug 2009
    Location
    Makati City, Philippines
    Beans
    2,169
    Distro
    Xubuntu 14.04 Trusty Tahr

    Loop the script

    Or should I say "re-run the script."

    I have this script:
    Code:
    #!/bin/bash
    cd /root/processing
    #loop to check if /root/processing folder still has files in it.
    while [ `ls /root/processing | wc -l` -ne 0 ];
    do
    #This is where I put the command to process each file and add to database
    file=`ls -tr1 | head -1`
    word1=`awk '{print $1}' $file`
    word2=`awk '{print $2}' $file`
    echo "INSERT INTO database.table(username, value) VALUES ('$word1', '$word2');" | mysql -uuser -puser
    #Move the file to completed directory.
    mv $file /root/completed
    
    done
    echo "All files processed."
    Basically, I have the folder /root/processing which contains text files to be added to the database in mysql. Then after processing the text file, it will be moved to the /root/processed folder. The script works fine if I run it.

    Here's my question, when there are no longer files in the /root/processing folder, the script stops. How can I run the script again automatically in case I will put text files in the /root/processsing directory?
    Last edited by nerdtron; January 23rd, 2014 at 10:10 AM.
    "I learned a lot from repeatedly breaking my system and then reinstalling. Oh, and a lot of GOOGLE searches too!"
    The best Ubuntu derivative: Linux Mint
    Relax. Head to the Community Cafe
    The Philippine Team

  2. #2
    Join Date
    Sep 2006
    Beans
    8,623
    Distro
    Ubuntu 14.04 Trusty Tahr

    inotify

    You might want to use inotify to watch your directory instead. It can trigger a script when files are added, changed or deleted in the directory. You would set up your run conditions using incron. It is in the repository and does not need a PPA but the rest of the HowTo is ok:

    http://www.howtoforge.com/triggering...es-with-incron

    The file format is a little confusing untill you see the pieces.

  3. #3
    Join Date
    Aug 2009
    Location
    Makati City, Philippines
    Beans
    2,169
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Loop the script

    Thanks for incrontab. I think it would be a good solution. I'll look into it.
    "I learned a lot from repeatedly breaking my system and then reinstalling. Oh, and a lot of GOOGLE searches too!"
    The best Ubuntu derivative: Linux Mint
    Relax. Head to the Community Cafe
    The Philippine Team

  4. #4
    Join Date
    Jul 2013
    Location
    Wisconsin
    Beans
    3,112

    Re: Loop the script

    Upstart-file-bridge, already part of Upstart. Example usage at http://upstart.ubuntu.com/cookbook/#upstart-file-bridge

  5. #5
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    10,893
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Loop the script

    Usually I just run the script from crontab with a short elapsed time like one or five minutes. In cases like the one the OP describes, I rarely have a need for real-time processing.

    If there's a chance the script may run longer than a minute, I write a lock file to /tmp. Then any subsequent attempts to run the script stop after checking for the presence of the lock file.

    Code:
    #!/bin/bash
    DEBUG=Y
    LOCKFILE=/tmp/script.lock
    
    if [ -f $LOCKFILE ]
    then
        [ "$DEBUG" = "Y" ] && echo "Lock file $LOCKFILE found."
        exit 1
    fi
    touch $LOCKFILE
    
    [etc.]
    
    rm -f $LOCKFILE
    
    exit 0
    You might consider spltting up the script into two parts. Create a generic "wrapper" script that runs from cron to check for the presence of the files. When found, the wrapper would write the lock file and invoke the SQL import script. This gives you a convenient template for handing similar processes in the future if you use variables for things like the monitored directory and the subsidiary script's name.
    Last edited by SeijiSensei; January 24th, 2014 at 05:26 PM.
    If you ask for help, please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

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
  •