Programatically created files become NULL in Linux

2019-09-21 01:41发布

问题:

This question is an exact duplicate of:

  • Newly created file becomes 0 kb (data gets overwritten to nothing) on reboot in Linux

I've a shell script which create files programmatically by writing the contents of a variable to a file in the memory location.

file=/downloads/fileName.crt
variable="These contents are to be
          written to the file"
echo "$variable" > "$file"

This works fine. I'm able to see that the file is written to and not NULL. But sometimes after running this script and after a reboot, the file contents become NULL. Why is this happening?

Link to full code:

Newly created file becomes NULL on reboot in Linux

This code is running on an industrial mobile Linux computer with very basic configuration.

回答1:

Try to

sync

at the end of the script.