0

My goal: run a Rscript when a file is created on directory /home/gabriel/data

Here's what I have with inotiwait

$ inotifywait -m -e create /home/gabriel/data |
  while read -r filename event; do 
    Rscript /home/gabriel/script/updatedb.R;
  done

I first run these lines on terminal, then create one new file on /home/gabriel/data. When the file is created, I can clearly see that my Rscript ran correctly because a database gets updated.

However, when I but the same code in bash I don't get my Rscript to run. Here's my bash:

#!/bin/sh
inotifywait -m -e create /home/gabriel/data |
  while read -r filename event; do 
    Rscript /home/gabriel/script/updatedb.R;
  done
Setting up watches.
Watches established.

Which I call inotifytest.sh and saved on /home/gabriel. I gave permission for this bash to be executed:

$ chmod +x inotifytest.sh

And then start running the bash:

$ sh inotifytest.sh
: not found.sh: 2: inotifytest.sh:
Setting up watches.
Watches established.

When I create new files, my database does not get updated which means that my Rscript did not run.

I'm probably doing it wrong in many levels but I don't see where. Thank you for your help.

Gabriel Mota
  • 302
  • 1
  • 10
  • (or) Can you paste the output when you type `file inotifytest.sh`. It could have DOS style line endings, if you had created the script on a DOS editor and running it on *nix – Inian Sep 07 '18 at 07:47
  • Did the `bash` change and got the same problem. My .sh file was indeed created on Windows and upload to Unix. Here's the output for `file inotifytest.sh`: `inotifytest.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators` – Gabriel Mota Sep 07 '18 at 07:50
  • 1
    `with CRLF line terminators` is the problem. DOS endings are CRLF and *nix are only LF. You need to clean them up by doing `dos2unix inotifytest.sh` and run the script – Inian Sep 07 '18 at 07:51
  • Great. You're right. Problem solved. Thank you. – Gabriel Mota Sep 07 '18 at 07:58

0 Answers0