{"id":8526,"date":"2024-11-15T01:06:00","date_gmt":"2024-11-15T06:06:00","guid":{"rendered":"https:\/\/www.both.org\/?p=8526"},"modified":"2024-11-15T16:30:16","modified_gmt":"2024-11-15T21:30:16","slug":"sync-the-command-you-never-heard-of","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=8526","title":{"rendered":"sync &#8212; The command you never heard of"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"8526\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>Have you ever heard of the <strong>sync<\/strong> command? Have you heard of it but then forgotten all about it &#8212; like me? The sync command is one that I tend to ignore most of the time. I mean, I have added it to many of my scripts but then never think of it again.<\/p>\n\n\n\n<p>But what does it do and why do we need it?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The basic problem<\/h2>\n\n\n\n<p>The root problem is that data isn&#8217;t written to storage devices at the instant we press the &#8220;Save&#8221; button in our LibreOffice applications. Nor does it happen with other applications. Our data is first stored in an area of RAM designated by Linux for cache. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is cache?<\/h2>\n\n\n\n<p>Cache speeds up writes from the applications because RAM is faster than disk.<\/p>\n\n\n\n<p>The fact is that the much slower spinning hard drives can&#8217;t always keep up with data writes from many system and user level programs all trying to save data at the same time. The program currently writing to the hard drive would block all the other programs so they could only proceed serially, one after another, until they all had their turn. By writing data to cache, the application and system programs can return to their tasks immediately so that there is only imperceptible delay. <\/p>\n\n\n\n<p>We need to know a little about buffers and the difference between them and cache. The bottom line is that there&#8217;s not much difference. They are both areas of RAM designated by the operating system to store data until it can be used by an application or placed in some form of storage like an HDD or SSD. They are so similar that our memory management tools like free, top, htop, and so on, don&#8217;t differentiate between them in their reporting.<\/p>\n\n\n\n<p>We can use the <strong>free<\/strong> command to display the combined amount of buffers and cache. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># <strong>free<\/strong>\n               total        used        free      shared  buff\/cache   available\nMem:        16367432      712616    14348668       11720     1627392    15654816\nSwap:        8388604           0     8388604<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">The immediate problem<\/h2>\n\n\n\n<p>One side-effect of caching is that it might not be clear to users when all of the cache has been committed to disk. This leaves open the possibilities that a forced power-off or reboot might cause the loss of data if that residing in cache has not been flushed to persistent storage. <\/p>\n\n\n\n<p>We assume that our data is safe once the program terminates or we press the &#8220;Save&#8221; button. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">sync to the rescue<\/h2>\n\n\n\n<p>The sync command is a simple one but it resolves that little problem for us. The sync program is designed to flush all data held in cache to storage, thus making it safe for us to reboot or shut down the host. The typical use by experienced SysAdmins after running programs like Vim or other text editors, updates, installations, and more, is to run sync twice in a row to ensure that all data has been flushed. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <strong>sync ; sync<\/strong><\/code><\/pre>\n\n\n\n<p>That&#8217;s it. The sync program produces no output. It does have a couple options you can use. The -d (data) option can be used to flush only the data and not any unneeded metadata. This can save time. The -f (file-system) option will sync the entire filesystem containing the cached files. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Is sync still needed?<\/h2>\n\n\n\n<p>The short answer is &#8220;yes.&#8221; <\/p>\n\n\n\n<p>The long answer is that the sync command was designed in a time when tape storage was in common use and disk drives were very expensive. It takes a relatively long time for cached data to be written to tape. The sync command was used to ensure that all data was flushed from the cache onto tape. Early disk drives were much slower than the ones we typically use on our personal computers today.<\/p>\n\n\n\n<p> However, I&#8217;ve seen the Linux shutdown wait on a busy program for a minute or so, but if the program doesn&#8217;t respond to a termination request after the timeout, the Linux kernel just terminates it, even if it hasn&#8217;t completed writing data to storage.<\/p>\n\n\n\n<p>I try to perform a clean shutdown of all the programs running on my system, then run <strong>sync ; sync<\/strong> and then do the reboot or power-off. I use the double sync after performing many tasks, despite the fact that a graceful shutdown of a modern Linux computer is supposed to flush all the cache to storage. <\/p>\n\n\n\n<p>That said, I&#8217;ve seldom encountered a problem that I could identify as being a result of uncommitted data being left in the cache. A few mangled files scattered over the last 28 years since I&#8217;ve been using Linux might have been caused by that, although there&#8217;s no way to prove it. <\/p>\n\n\n\n<p>Today&#8217;s fast systems and hard drives, along with the advent of blazing fast SSDs make these issues much less common. But there is still a slight probability that data loss might still occur. <\/p>\n\n\n\n<p>So my final, unequivocal answer is &#8212; maybe. Better safe than sorry.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever heard of the sync command? Have you heard of it but then forgotten all about<\/p>\n","protected":false},"author":2,"featured_media":8532,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[5,89],"tags":[472,344,97],"class_list":["post-8526","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-system-administration","tag-linux-commands","tag-storage","tag-sysadmin"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8526"}],"version-history":[{"count":17,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8526\/revisions"}],"predecessor-version":[{"id":8567,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/8526\/revisions\/8567"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/8532"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}