Author Topic: Script Question  (Read 829 times)

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6852
  • MLU
Script Question
« on: October 05, 2010, 09:25:44 AM »
This refers to something I recall happening some time ago, but would like to have some input for learning purposes.

In a Bash script, copying a large file to a USB flash drive, the copying process completes ......  but of course the write of the file to the drive is not completed, as the file is actually cached I understand, and will continue to be written to the drive long after the command completes.

This presented a problem, as the size of the file is unknown, as is the speed of the write to the drive, so no allowance could be made for the time it would take.

Now if that copy command was followed by another command which used that file (I think that was the case), the second command will kick in while the writing is still happening, as the copy command itself has completed.

I think that was the situation that presented itself.

IIRC, it was worked around by doing another small write to the drive immediately after the copy, which did not begin until the drive was free to be written to, and completed without needing cache.
Thus the real second command could be executed as the write of the file was definitely completed.

I hope I have remembered that correctly ......  and been able to explain it properly.

My question then is this .......  is there a 'proper' means of determining that a copy and write to such a drive has completed before the next command is executed?

Maybe something obvious that I just do not know about, or remember?


regards.

Offline Joble

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6804
  • USA - Mountain Time
Re: Script Question
« Reply #1 on: October 05, 2010, 09:36:37 AM »
Maybe something like this?
http://linux.die.net/man/8/sync
Search First.
Forum Rules
Hero means I talk a lot, nothing more, nothing less!
Have an Awesome Day!
Healthy System

vjeko

  • Guest
Re: Script Question
« Reply #2 on: October 05, 2010, 10:20:36 AM »
you could do safe remove, it ll make sure that data write and cache clear is done, but will leave you in a limbo as far as the future actions ;)

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6852
  • MLU
Re: Script Question
« Reply #3 on: October 05, 2010, 10:30:28 AM »
Maybe something like this?
http://linux.die.net/man/8/sync


Thanks.

mmmmmn ......  seems to be using a sledgehammer to crack a nut .......  unless I missed something that could be used for specific file/s rather than the whole system ......

vjeko

  • Guest
Re: Script Question
« Reply #4 on: October 05, 2010, 10:32:44 AM »
could you instruct the sync to apply only to a specific drive, by including that in your script?

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6852
  • MLU
Re: Script Question
« Reply #5 on: October 05, 2010, 10:33:29 AM »
you could do safe remove, it ll make sure that data write and cache clear is done, but will leave you in a limbo as far as the future actions ;)

Yes thought of that ......  but the present solution is neater I think ....  just force the wait for the write to complete, by issuing another small write and delete the temporary file.

I thought there might be something along the lines of 'wait' ..... or something like that ....  that I have no knowledge of ...

Thanks for the suggestion.   ;)


Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6852
  • MLU
Re: Script Question
« Reply #6 on: October 05, 2010, 10:35:36 AM »
could you instruct the sync to apply only to a specific drive, by including that in your script?


I have not seen that option .....


Code: [Select]
$ sync --help
Usage: sync [OPTION]
Force changed blocks to disk, update the super block.

      --help     display this help and exit
      --version  output version information and exit

Report sync bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report sync translation bugs to <http://translationproject.org/team/>

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6852
  • MLU
Re: Script Question
« Reply #7 on: October 05, 2010, 10:41:02 AM »
Maybe fsync could be used ......  it seems the method previously used is as simple as it gets   ;D

I like simple   :D

Offline kjpetrie

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 4003
Re: Script Question
« Reply #8 on: October 05, 2010, 03:36:06 PM »
I would have thought if you simply chain the copy command to the read with '&&' that would require the copy to be completed successfully before the read is attempted.

That wouldn't necessarily mean it was all written to the drive though, as the end of the file might still be in cache, but it should be accessible for reading. As others have said you would have to eject or unmount the drive to guarantee a complete write to the hardware.
-----------
KJP
-----------------------------------------------------------
PClos64 RC1 on Intel D945GCLF2 motherboard (Atom 330), 2GB DDR2 RAM, Maxtor STM325031, HL-DT-ST DVDRAM GSA-H42N, Amilo LSL 3220T monitor. Also Acer 5810TG (with custom kernel) and Asus eeePC 2G surf

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6852
  • MLU
Re: Script Question
« Reply #9 on: October 05, 2010, 03:54:43 PM »
I would have thought if you simply chain the copy command to the read with '&&' that would require the copy to be completed successfully before the read is attempted.

That is exactly the problem I have described .......  the copy is completed but the writing of the file is not .......  so there is an incomplete file on the drive when the next command attempts to run.


Quote
That wouldn't necessarily mean it was all written to the drive though, as the end of the file might still be in cache, but it should be accessible for reading. As others have said you would have to eject or unmount the drive to guarantee a complete write to the hardware.


The other method I described works fine without having to eject the drive.


So far nothing easier has presented itself ........