Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have written a bash script which detects if a .ldif file has been written into a directory and if written, executes an ldapadd command and then deletes that file. The scripts is as follows:

dir="/home/myuser/newldif/"
        while inotifywait -e create "$dir"; do
              ldapadd -w "ldappassword" -D "cn=Manager,dc=mydomain,dc=com" -f /home/myuser/newldif/user.ldif
              rm -rf /home/myuser/newldif/user.ldif
        done

The script is executed like so:

nohup ./testscript &

The directory newldif is initially empty. When the file user.ldif is copied to this location, the script executes but skips the ldapadd command and executes the rm command.

On the other hand, when I execute the script without inotify (i.e. I removed the while loop completely), it adds the ldap entry and then deletes the file.

Is there anything wrong with the inotify syntax? The slapd logs don't show any errors. The nohup.out file simply shows the following output:

Setting up watches.
Watches established.
Setting up watches.
Watches established.
Setting up watches.
Watches established.

(each "Setting up watches." statement is for one deleted file)

The OS is CentOS 6.4 and OpenLDAP version is openldap-2.4.23.

share|improve this question
    
Force bash with a #!/bin/bash as the first line of the script. Add a set -x to your script to get a trace of what's happening. FWIW this works as expected on a system I have to hand. –  Iain Aug 23 '13 at 8:23
    
@Iain - #!/bin/bash is already the first line of the script. Where do I add the set -x? –  i.h4d35 Aug 23 '13 at 8:27
    
second line is a good start. –  Iain Aug 23 '13 at 8:48
    
Ok. This is the output after including set -x - ldapadd -w ldappassword -D cn=Manager,dc=mydomain,dc=com -f /home/myuser/newldif/user.ldif - For some reason, the quotes around the password and the rootdn have been removed. Is that what is causing it to skip the ldapadd command? –  i.h4d35 Aug 23 '13 at 8:58

1 Answer 1

up vote 1 down vote accepted

If the rm is executing from this script then there is nothing wrong with your logic.

It's probably a timing issue where the file is created in the directory but not populated when your script runs the ldap command. Try adding a cat of the file to see. Then try waiting for a close_write event on the directory instead so you have a fully populated file. This may also not be sufficient, as it depends on how you write to the file. Generally the safest way is to create a temporary file elsewhere then move that into place when fully populated, as this is a more atomic file system operation.

Also, adding some error checking for files you expect or commands is also a good idea:

#!/usr/bin/env bash

dir="/home/myuser/newldif/"
file="user.ldif.tmp"
ldf="$dir/$file"

while inotifywait -e close_write "$dir"; do

  # Check if the file we want exists in the directory being monitored. 
  if [ ! -f $ldf ]; then 
    echo "No file [$ldf]"; 
    continue;
  fi

  # Add users via ldap
  echo "Adding users:"
  mv $ldf $dir/user.ldif
  ldapadd -w "ldappassword" -D "cn=Manager,dc=mydomain,dc=com" -f $dir/user.ldif || echo "failed adding users" && exit 1

  # Remove file when done
  echo "Removing file"
  rm $dir/user.ldif || echo "failed removing file" && exit 1
done
share|improve this answer
    
The other commands like echo, rm, cp, mv and ls (these are the ones I tried) work within the while loop. As far as writing to the file is concerned, the file in it's entirety is being copied from another machine directly into this location. –  i.h4d35 Aug 23 '13 at 9:13
    
that same ldap command works outside of the script though? –  mtm Aug 23 '13 at 9:20
    
yes. Also, if I run the script without the while inotifywait..., it adds the ldap entry successfully. –  i.h4d35 Aug 23 '13 at 9:23
    
Sorry, what is inotifywait...? If you're copying a remote file then, the file is created locally, then copied over the network, then closed. You are watching the 'create' event. Trying copying to user.ldif.tmp, then moving to user.ldif. Alternatively, what does code above produce? –  mtm Aug 23 '13 at 9:28
    
The above code doesn't give any output (cat $ldf). I am trying your suggestion of a temp file as we speak. –  i.h4d35 Aug 23 '13 at 9:38

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.