I run an Ubuntu system, and it sometimes takes a while to shut down. Apparently, GNU/Linux attempts to, in a nutshell, stop all running processes, flush the filesystem journal, and then cut off the power. My reasoning is that, when power is removed, all data kept in RAM is lost, and therefore the system does not need to manually kill the processes as they will die anyway. Would running sudo sync and then removing power be a safe (and significantly faster) alternative to a proper shutdown (shutdown -h now), or would it present a hardware/security danger?

How long is "a while"? If it is more than 10 seconds there is something abnormal going on, you should get out of X and halt from a VT to see what's up. Ps. your idea is a bad one and will lead to filesystem corruption if you make it a habit.
–
goldilocksDec 29 '12 at 23:11

2 Answers
2

Doing sudo sync ensures that data that has been written by applications but remains in a memory buffer is flushed to the disk. That's all it does. It does not shut down the system properly and is likely to result in data loss and other mishaps. This isn't catastrophic — power failures are known to happen — but it's best to avoid this. Here are things that a proper shutdown does:

Notify applications that the session is terminating. Applications may prompt you to confirm the shutdown, and may perform an emergency save to a file. They may also remove their temporary files, which would otherwise accumulate.

Shut down network connections properly. If connections are interrupted suddenly, there will be a time during which the other side is unaware that your computer has gone and will keep trying to send data to or expecting to receive data from a connection that will not see any life again (even if your computer reboots).

Write log entries confirming what happened during the shutdown. This is useful if something goes wrong and you need to investigate.

Unmount all filesystems. This marks the filesystems as clean. You trade a tiny bit of time during shutdown for a larger (but still small on a human scale1,2) time during the next boot, plus a lower risk of hitting a bug (because sudden failure cases are not as thoroughly tested as normal operation).

¹ Except possibly for RAID volumes: if a RAID volume needs to be verified, that can take hours — it's done in the background, so it isn't blocking, but it does cause a performance hit.
² Except for older, non-journaled filesystems such as ext2 which require a lengthy fsck if not unmounted cleanly.

No, in general it would not be safe. The sync writes pending data (data from write operations that are currently in buffers) to disk. But if you haven't shut down the applications there may still be data that the application hasn't yet written.

Imagine a GUI program which reads in a configuration file at start and writes the configuration file when the program quits. If you change a setting in the GUI a sync will not help because the application has not yet written this data.

Another example is a torrent client. A sync would write the latest data to the disk, but it would not disconnect from the overlay network. That's not polite let the connection just time out from the other side, if possible you should announce to the overlay that you're leaving.

You should usually give programs a chance to properly shut down.
Having said this, there are cases in which it does not make a difference. However, the file system may still be marked as dirty if it's not been properly unmounted.