0

I need to read and write serveral files in serveral remote machines. Mainly using java, and shell is also ok.

At first, I want to ssh to the remote machine and read/write file, and using Channel.lock() to lock the file. But I found it difficult to implement...

Then, I have another way:

For every remote file, such as FileA, provide a shell script, such as changFileA.sh on the remote machine in advance.

Thus, I can concurrent execute the tasks. Each task contained the following info.

machineIp, filePath, changeScriptPath, changeParam

But the question is that, there will be more than one task to modify the same file at the same time.

How I can lock it? Only allow one thread to modify the same file?

Thank you.

NingLee
  • 1,477
  • 2
  • 17
  • 26
  • The linked question is about implementing file locking from shell scripts. You clearly already know how to run a shell script remotely (using ssh), and how to run an external command from Java. So you should be able to put all 3 things together. – Stephen C Oct 22 '14 at 07:55
  • Thus, I must lock the file in shell script, at first, I wanted to lock the file in java programs. Both are OK. – NingLee Oct 23 '14 at 10:50
  • It depends on what you are locking against. The semantics of `FileLock` are platform specific. Read the javadocs carefully. Note also that `FileLock` is NOT suitable for a use-case where two threads in the same JVM are locking against each other. (Again, read the javadoc.) – Stephen C Oct 23 '14 at 11:43

0 Answers0