Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

First, I should note that I am pretty new to writing scripts, but I know my way around ubuntu well enough that I don't mind getting my hands dirty...

I have a small script, which does a simple directory watch and when it finds a new file in the directory, copies that file to another directory. The script I am using looks like this:

#!/bin/sh
#
sudo rsync -av --omit-dir-times --delete --progress /home/sunmorgus/tempx/Complete/ /media/complete/

while [ 1 ]
do
  echo 'Watching directory: '$WATCHED_DIR 'for new files'
  while file=$(inotifywait -q -e create "$WATCHED_DIR" --format "%f")
  do
    echo 'New file to upload to moonback' $file
    notify-send -i "gtk-go-up" "Complete Folder Monitor" "Uploading file $file"

    #rsync goes here
    sudo rsync -av --omit-dir-times --delete --progress /home/sunmorgus/tempx/Complete/ /media/complete/

    notify-send -i "gtk-home" "Complete Folder Monitor" "File uploaded."
  done
done

It's a modification of a script I found here: http://www.webupd8.org/2010/11/script-to-watch-folder-and-upload-new.html

I have a few questions about the script. First off...is this the best way to do what I am trying to do? Like I said, it works fine, but I'm always open to new ideas. Note: the destination directory is an SMB share on a Windows 7 machine.

Second...what would be the best way to run this script at startup? Right now, I went into the "Startup Applications" screen and added a new record that runs this line "gnome-terminal -e /script.sh" which works, but doesn't seem ideal. I tried adding the script to the init.d folder and running update-rc.d, and adding the script to /etc/rc.local but neither of those worked (I got no errors, but the script wasn't running after a reboot).

Lastly...is there a way to modify this script so that it also deletes from the destination when deleted from the source? The script only runs when files are added, not deleted, and while the rsync portion will delete from the destination when deleted from the source, it won't do it until a new file is added. (hopefully that paragraph makes sense).

share|improve this question
add comment

1 Answer

For your questions...

  1. This will work reasonably well. If you want to get fancy, look into famd.
  2. Add it to the end of /etc/rc.local or create proper init scripts according to your distro's standard method.
  3. The --delete option to rsync should do that already.
share|improve this answer
    
2. I tried adding it to the rc.local...it didn't work. I could try again i suppose... 3. I already have the --delete, but the inotify part of the script doesn't recognized deletes, so it will only delete files when a new file is added. I need to know how to make inotify recognized adds and deletes... –  Nick DeMayo Jul 29 '11 at 17:56
    
@sunmorgus Then you'll have to create proper init scripts. The particulars will depend on which distro you're using. I'm not sure what to do about inotify. Maybe you'll want to switch to fam anyway. –  bahamat Jul 29 '11 at 19:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.