How to hibernate system using systemd

Hibernate desktop system using systemd.

Ubuntu version.

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu Focal Fossa (development branch)
Release:        20.04
Codename:       focal

Hibernate operating system.

$ sudo systemctl hibernate

Inspect service status.

$ systemctl status systemd-hibernate
* systemd-hibernate.service - Hibernate
     Loaded: loaded (/lib/systemd/system/systemd-hibernate.service; static; vendor preset: enabled)
     Active: inactive (dead)
       Docs: man:systemd-suspend.service(8)

Mar 06 16:35:05 desktop systemd[1]: systemd-hibernate.service: Succeeded.
Mar 06 16:35:05 desktop systemd[1]: Started Hibernate.
Mar 06 23:25:17 desktop systemd[1]: Starting Hibernate...
Mar 06 23:25:17 desktop systemd-sleep[165956]: Suspending system...
Mar 07 10:15:15 desktop systemd-sleep[165956]: System resumed.
Mar 07 10:15:15 desktop systemd-sleep[166119]: /dev/sda:
Mar 07 10:15:15 desktop systemd-sleep[166119]:  setting Advanced Power Management level to 0xfe (254)
Mar 07 10:15:15 desktop systemd-sleep[166119]:  APM_level        = 254
Mar 07 10:15:15 desktop systemd[1]: systemd-hibernate.service: Succeeded.
Mar 07 10:15:15 desktop systemd[1]: Started Hibernate.

Inspect service details.

$ cat /lib/systemd/system/systemd-hibernate.service
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Hibernate
Documentation=man:systemd-suspend.service(8)
DefaultDependencies=no
Requires=sleep.target
After=sleep.target

[Service]
Type=oneshot
ExecStart=/lib/systemd/systemd-sleep hibernate

So now you know to read systemd-sleep manual page for more detailed information.

Inspect systemd sleep configuration file.

$ cat /etc/systemd/sleep.conf
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See systemd-sleep.conf(5) for details

[Sleep]
#AllowSuspend=yes
#AllowHibernation=yes
#AllowSuspendThenHibernate=yes
#AllowHybridSleep=yes
#SuspendMode=
#SuspendState=mem standby freeze
#HibernateMode=platform shutdown
#HibernateState=disk
#HybridSleepMode=suspend platform shutdown
#HybridSleepState=disk
#HibernateDelaySec=180min

Inspect /usr/lib/systemd/system-sleep/ directory to learn how to create additional shell scripts that will be executed pre or post specific operations.

$ cat  /usr/lib/systemd/system-sleep/unattended-upgrades 
#!/bin/sh
set -e

if [ "$2" = "hibernate" ] || [ "$2" = "hybrid-sleep" ]; then
    case "$1" in
        pre)
            /usr/share/unattended-upgrades/unattended-upgrade-shutdown --stop-only
            ;;
    esac
fi
$ cat  /usr/lib/systemd/system-sleep/hdparm 
#!/bin/sh

case $1 in
  post)
    /usr/lib/pm-utils/power.d/95hdparm-apm resume
    ;;
esac

That should get you started.