3

I have a Java application which works on X number of text files. When file processing is over it transfer the files to a destination folder.

I have a bash script which picks files from the destination folder and sends them to another server at a scheduled time.

I have to implement synchronization between the Java application and the shell script. Is it possible?

6
  • What do you mean by synchronization? Do you mean you want the shell script to run after the Java app is finished (easy)? Or do they both run concurrently and you want them to somehow coordinate with each other (hard)? Commented Oct 25, 2019 at 10:44
  • It means that the shell script should not start sending incomplete files that the Java application is still in the process of writing. Commented Oct 25, 2019 at 10:46
  • You could use "inotify" to watch for file changes in destination folder Commented Oct 25, 2019 at 10:46
  • What kind of synchronization? Does the bash script need to know that the Java application is done on all files? Make the application create a lock file before it begins and remove it when done. Let the script check the presence of this file. Commented Oct 25, 2019 at 10:46
  • hi @Kayaman senario: bach processing Commented Oct 25, 2019 at 10:48

1 Answer 1

3

I have to implement synchronization between the Java application and the shell script. Is it possible?

You can do implicit things, like: the Java application puts a marker file like processing-files-now.txt into the destination directory. Just for better debugging, that file might contain the number of files that are currently processed.

When the Java application is done processing, it can come in and delete that marker file.

Then the script could check for the presence of that file (in order to not start transmitting files while the java application adds new files).

On the other hand, the real problem might be: the java application starts while the script is transferring files. If you really want to avoid that, you could have the script write such a file, too.

But note that such "file system based" synchronisation isn't too robust, and might give you all kinds of trouble over time.

This really depends on "scale". For a small setup, where there is a low chance of serious trouble, or even no terrible for consequences when the two programs work in parallel ... the above might do.

But in a more real world scenario, you should do such things differently.

Sign up to request clarification or add additional context in comments.

3 Comments

If necessary, this marker file could also be per-file (so that the script can send the files that are already done, but skip the ones that are still being written). Or you could check the last modification timestamp for the files and assume that if nothing has changed in X minutes, it is done.
@GhostCat if i have to implement IPC between shell script and java application is it possible?
@user3559721 Shell scripts are really not a good ingredient for that. I would step back and look at doing this in different ways. But that would be a much broader discussion, and that is not what this community is about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.