The Safe Sleep mechanism MacOS
I don't know about MacOS, so I take your word for it. The process you describe has three steps:
- System enters sleep (also called suspend to RAM) when you close the lid or press a button or click on an icon.
- If battery gets critically low, the system wakes up and saves the system-state in disk, thus entering into the hibernate mode and then powers off.
- When you turn the system back on again it starts in the same state it was left in, with all the applications open and unsaved files intact.
If you want Ubuntu to behave exactly like MacOS as described above, there is no way to do it.
Ubuntu Does It Defferently
The difference is in step 2. Ubuntu has no way of waking the system and saving the state in the disk when the battery is critically low. However, it can get the same results in two different ways. I will describe them below:
Hybrid-sleep
This method is the closest to "Safe Sleep" in MacOS
In this method, at step 2, when you close the lid, first thing Ubuntu does is, save the state of the system (which applications are open, etc.) into the disk. Once all that information is safely in the disk (either in the swap file or partition depending on your setup) Ubuntu suspends to RAM or more simply put, goes to sleep.
If you open the lid before battery gets too low, then the laptop wakes up from sleep as normally and as quickly as in the normal sleep mode. The information saved in the swap file/partition is discarded in the background. This process is usually faster than booting the laptop.
If battery gets critically low, Ubuntu will shutdown. However, since the information is already saved, it will not be normal shutdown. Instead it will hibernate. That is, when you power up the laptop again it will boot up at the state you left it when you closed the lid.
The disadvantage of this method is, it takes longer for the laptop to initially go to sleep after you close the lid as it takes a bit of time to save the information in the disk first. So, when you close the lid, the LED lights on the laptop will continue to glow for a while, before the lights change to indicate that the laptop has entered in the sleep mode.
If the system runs out of power, hybrid-sleep will restore it to the state you left it in as you want.
suspend-then-hibernate
This is similar to hybrid-sleep but with a twist. In this mode laptop enters the sleep mode as soon as you close the lid. However, you configuration you setup a time period (say one hour). After one hour has passed, the laptop saves the information in the RAM to the disk, and then turns itself off.
If you come back to your laptop within an hour (or the time you set), when you open the lid, it will wake up from sleep quickly as usual.
If you go for lunch and meet an old friend and decide to take the rest of the day off, and come back to work the next day, you will be able to turn the laptop back on without plugging it in first (assuming it had some juice left after an hour of sleep) and start working where you left off the previous day before lunch.
The disadvantage is if you return from lunch five minutes past the hour, it will take longer for the laptop to spring back to life (in the state you left it), as it has transitioned from sleep to hibernate mode.
The system does not run out of power as it turns itself off after the set time and restore itself to the state you left it when you turn it back on.
Both Solutions
Both solutions are described in the answer How do I use pm-suspend-hybrid by default instead of pm-suspend?
This is why I consider this question a duplicate. However, I wrote this answer to make it clearer based on the comments and updated question.
Hope this helps