How do you detect if the file is in deferred state?

How do I know if a file or files are in deferred state using bash? Is there a way to detect this?

sample:

when saving a file, kernel cache the file and not yet written on the disk. this is the D state or deferred state. when its deferred state, when you check the file like php -r "var_dump(file_exists($filename));", the kernel will respond, yep the file is there. but in reality, the file wasn't written on the disk yet. and when the server got restarted, the file is gone.

the event happened to us and i want to prevent it or create a monitoring tool that detects it and notify us so we can copy the files on other server before restarting it.


Solution 1:

I think you are looking at a wrong "problem". When system is properly restarted, the shutdown process will make sure that all data is written to permanent storage.

So, the problem here is that restart is done in wrong way, which causes data loss. You need to change your restart to use shutdown -r now or similar command.