1

I have a windows application will create a .xml file and upload it to a folder on my Linux server. I need a way to watch for this new file, its filename would be something like "004job.xml", "012job.xml" etc,etc. The filename is different every time. After the file is detected, a script would be run with the new file as its command line variable. Like this "/home/project/scripts/renderjob.sh /home/project/jobs/012job.xml". The script will do some actions based on the contents of the .xml file and the last action is to delete the .xml file.

Its also important that only one instance of this script is running at a time. but that is a horse of a different color.

Thanks for reading.

  • This might help: [How to continuosly monitor the directory using dnotify /inotify command](http://stackoverflow.com/q/7566569/3776858) – Cyrus May 08 '16 at 17:20

2 Answers2

0

Briefly:

After the file is detected, a script would be run with the new file as its command line variable.

https://unix.stackexchange.com/questions/273556/when-a-particular-file-arrives-then-execute-a-procedure-using-shell-script/273563#273563

Its also important that only one instance of this script is running at a time.

Use flock in your script in this way: https://unix.stackexchange.com/questions/274498/performing-atomic-write-operations-in-a-file-in-bash/274499#274499. Insert watching for your file inside the flock:

(
    flock -s 200

    res=$(inotifywait $DIR)
    # other actions

) 200>/var/lock/myscriptlock 
Community
  • 1
  • 1
  • This works somewhat. But how can pass just the filename created to the script. The $RES contains the event "/home/project/jobs/ CREATE 34job.xml. I just need the $RES to contain only the filename created. And i need this to watch all the time, even after an event is triggered. Keep watching. Thanks – Randy Adams May 08 '16 at 18:47
  • 1) `how can pass just the filename created to the script?` Why not monitor file events for a folder? Are you expecting someone else creating files in the same folder? 2) `I just need the $RES to contain only the filename created.` Then use `basename` on Linux. 3) `And i need this to watch all the time, even after an event is triggered.` Then let your script do a loop –  May 08 '16 at 19:32
0

You can use Inotify-command, Docker available here, Github project here.

This tool allow u to watch a folder for new files, and lunch a custom commande / script if a new file or, new version of the file is detected.

sudo docker run --name=inotify-command -d \ 
-v /etc/localtime:/etc/localtime \
-v /config/dir/path:/config:rw \
-v /dir/path:/dir1 \
martinbouillaud/inotify-comman:latest

Option IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING=0 allow u to run only one instance of the script at the time.

In your case, you could make something like :

sudo docker run --name=inotify-command -d \ 
-v /etc/localtime:/etc/localtime \
-v /home/project/scripts:/scripts:rw \
-v /home/project/jobs:/jobs:rw \
-v /home/project/config:/config \
martinbouillaud/inotify-comman:latest

Then, create a file job.conf and put it in your config folder with :

WATCH_DIR=/jobs
SETTLE_DURATION=5
MAX_WAIT_TIME=30
MIN_PERIOD=30
COMMAND="/scripts/renderjob.sh"
# Need to run as root to have the authority to fix the permissions
USER_ID=0
GROUP_ID=0
UMASK=0000
# This is important because chmod/chown will change files in the monitored directory
IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING=1
USE_POLLING=no

Finaly you need to adapt your renderjob.sh to work inside the Docker with paths /jobs and /scripts ...

Hope it can help u.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 14 '22 at 13:42