13

I know has been a bit of discussion about topics similar to this. But here is what I am basically trying to do.

I have a watch directory called watched and whenever a file is added to that directory, I want to trigger a script called syncbh.sh which will take files out of that directory and upload them to a remote server.

The caveat is that files are created in the watched directory by one user (user2), but the script is executed by another (user1).

I've tried using incron to accomplish this, but keep running into a major problem because while the script can be executed manually by user1 with root privileges, the incron daemon is never actually automatically triggered by a file creation event by the other user2.

I've thought about whether inoticoming would be a better alternative, but I'm unclear about how the syntax of this works. If there a better way to accomplish this, or if I end up using inoticoming what would the command syntax be to ask it to watch /home/user1/watched directory and execute the script /usr/local/bin/syncbh.sh if a file is created/modified within that directory?

Any help would be much appreciated.

3
  • Which user must run the script?
    – A.B.
    Apr 23, 2015 at 11:23
  • And as which user should run inoticoming?
    – A.B.
    Apr 23, 2015 at 12:03
  • Please accept the answer that fits your needs and I'll give the bounty to that answer.
    – 0x2b3bfa0
    Apr 26, 2015 at 19:55

3 Answers 3

5
+50

Using inoticoming:

You can put a script in /etc/init.d/ that runs inoticoming at boot time.

  1. Create a new folder to hold the inoticoming log / last pid for the watched folder: sudo mkdir -p /var/log/inoticoming/watched/

  2. Create a script inoticoming_watched in /etc/init.d/:

* Remember to change <path_to_folder> and <path_to_script> to match the full path of the watched folder and the full path of the script to execute

#!/bin/sh

case "${1}" in
    start)
        inoticoming --logfile '/var/log/inoticoming/watched/inoticoming.log' --pid-file '/var/log/inoticoming/watched/inoticoming_last_pid.txt' <path_to_folder> <path_to_script> \;
    ;;

    stop)
        kill -15 $(< /var/log/inoticoming/watched/inoticoming_last_pid.txt tee)
    ;;

    restart)
        ${0} stop
        sleep 1
        ${0} start
    ;;

    *)
    echo "Usage: ${0} {start|stop|restart}"
    exit 1
    ;;
esac
  1. Mark the script as executable: sudo chmod u+x /etc/init.d/inoticoming_watched

  2. Make sure that the script called by inoticoming_watched is executable.

  3. Update rc.d to make the service inoticoming_watched start at boot time: sudo update-rc.d inoticoming_watched defaults

You can check the inoticoming log in /var/log/inoticoming/watched.

4

First of all, install inoticoming:

sudo apt-get install inoticoming

Then use this command:

Pay attention to ongoing processes of inoticoming, because they can be started multiple times.

$ inoticoming /home/user1/watched /usr/local/bin/syncbh.sh /home/user1/watched/{} \;
              ^                   ^                        ^
              |                   |                        |
              ^-- The directory to be monitored            |
                                  |                        |
                                  ^-- Your script          |
                                                           ^-- The parameter for your script
  • The process runs in backgound and is watching /home/user1/watched

  • When a file is added or changed in that directory, the script /usr/local/bin/syncbh.sh is called.

    • The parameter for this script is in this case /home/user1/watched/<name_of_changed_or_modified_file>

    • {} is replaced by the filename

-2

First of all, a script to watch the watched directory:

#! /bin/bash

folder=/path-to-watched

inotifywait -m -q  -e create  -e modify  '%:e %w%f' $folder | while read file
  do
    #make the sync here
  done

Second To make sync as another user (user2) the :

sudo -H -u user2 bash -c 'sh /usr/local/bin/syncbh.sh ' 

Now in order to not make a user prompt, you can set your sudo password in a file and read the password from this file when needed (note you have to use -S with sudo to get the password from file the).

Put your sudo password in a file, suppose passwd.txt, then the command above will be suck like

sudo -S -H -u user2 bash -c 'sh /usr/local/bin/syncbh.sh ' < /path-to/passwd.txt

Now the overall script will be like:

#! /bin/bash

folder=/path-to-watched

inotifywait -m -q  -e create  -e modify  '%:e %w%f' $folder | while read file
  do
      sudo -S -H -u user2 bash -c 'sh /usr/local/bin/syncbh.sh ' < /path-to/passwd.txt      
done
1
  • 1
    -1: Never put the password in a plaintext file.
    – 0x2b3bfa0
    Apr 30, 2015 at 9:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .