help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] When is it useful to use "sync"?


From: Gotmy Nick
Subject: Re: [Help-bash] When is it useful to use "sync"?
Date: Thu, 26 Sep 2013 16:19:40 +0200

On 25 September 2013 17:59, Greg Wooledge <address@hidden> wrote:

Putting sync into a script is most likely not useful, unless the script
is part of a full operating system shutdown or something.  Ask your
friendly neighborhood OS experts for advice if you think you're in a
situation where data loss is possible.


I would say, it's not only not useful, but also it is intrusive. Unless the script in question has a global scope (to perform something against all the running processes) in the system.

I mean, if you call sync after your cp from your script, it's not going to sync only your copied file, but also _all_ processes with pending I/O.

In a home server without load, this may be imperceptible, but on a complex production system working with serious data, may impact services.

Nowdays, the I/O of processes and systems, maybe pretty complex. Think on mysql or a web server with many virtualhosts and logs. Those applications, have many logic about I/O, buffering, caching, open files, and data integrity.

A sync maybe innocuous and convenient, or may put the system I/O (and so the system services) down. This usually is going to depend on scenarios, external factors, environment, and running processes, and this is difficult to track from a script

There are many layers for I/O. For example, in linux, you got the kernel schedulers (you can choose many), the kernel sysctl (look for dirty_background_ratio and friends), the mount options for the filesystem (which maybe already mounted in sync mode if it's so critical), etc etc etc...

Even on system shutdown, sync should be already called by umount.

I use to call sync on my desktop, usually when dealing with USB drives, because many times (working in more than one task at the same time) I've forget to umount them properly before disconnect the drive.

For the specific question: "after copying a file with "cp" is it safe to use "cat" on the same file?"

The response is: if cp did return success, yes, it's safe. Of course, you still may encounter corner cases, like a failing disk, a failing sector, etc in the middle of the operation.

And about: "Is this always guaranteed, or is it better to use "sync" after "cp"?"

Access and operations on the file are guaranteed to work without you calling to sync first.

What is not guaranteed, is that your OS caches and buffers, have preserved that data physically in the next nanosecond after cp returns.

On current systems, with journaling filesystems, I think it's pretty difficult to loose data by not call 'sync' manually after a 'cp' (lets skip drive removals without umount in this consideration).


--
Iñigo Tejedor Arrondo

reply via email to

[Prev in Thread] Current Thread [Next in Thread]