Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is a hypothetical scenario. Say that I have a particular folder that is being synchronized by a process which is receiving a files/folders via the internet. Now while this operation is in progress, for the synchronizer's sake, there should be no cp operations to its directory. Anyway this can be acheived?

share|improve this question
Do you have control over what starts the cp process? Do other programs need to be able to write to the directory while it's being synchronized? What about reading? Why is this an issue: what are your requirements in terms of consistency? – Gilles Jul 8 '12 at 0:42
up vote 1 down vote accepted

Your synchronizer is writing to the directory rather than reading, right? So taking a filesystem snapshot or setting the immutable bit is out.

If you're running the synchronizer as root, you can change the ownership of the directory to root and the permissions to 700 (rwx------; only owner can do anything). This will keep out any new processes not running as root, but any process that already has a file or subdirectory open will still be able to make changes.

On Linux, if you want to find all processes that have currently files open in your tree, you can do lsof +D my_directory. What you do with that knowledge, though, depends on the situation.

share|improve this answer

There are at least two possible solutions:

  1. Create a wrapper script for cp which checks if the synchronization is running, either by checking if the process is running or testing a lockfile, and abort or delay the operation.
  2. Use LD_PRELOAD to hook up write and other calls.

The problem with first solution is that you'd have to wrap all commands used to write into the folder, e.g. mv, cp, rsync, rm, ln, cat … On the other hand it is quite simple but will also work only for your current user.

For the second solution you'd have to write a shared library with all the syscalls used to change/modify the directory, e.g. write, unlink, rename. But this solution would work with all dynamically linked executables - so no need to adjust every other program.

share|improve this answer

If you know that the synchronization process is running and know that the only possible operation is cp, then you could send SIGSTOP to all processes to pause execution. After the synchronization completes, you send SIGCONT to all processes to continue.

For that purpose, you can use kill -s SIGSTOP $(pidof cp) or killall -s SIGSTOP cp. In a terminal, you can use Ctrl + Z to background a task ("pause"). To continue execution, use the fg command ("foreground", see help fg).

share|improve this answer

Your Answer


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.