The other answer is great! But it requires root cron.
If you want to hibernate from non-sudo cron, there are 2 options:
Make a file containing the following:
[Enable hibernate to be run via cron]
com.0.enable-hibernation-from-cron.pkla in the directory
Explanation is given here.
Quoting from here:
If users should only be allowed to use shutdown commands, but not have
other sudo privileges, then, as root, add the following to the end of
/etc/sudoers using the
user hostname =NOPASSWD: /usr/bin/systemctl poweroff,/usr/bin/systemctl halt,/usr/bin/systemctl reboot
user for your username and
hostname for the machine's hostname.
Now your user can shutdown with
sudo systemctl poweroff, and reboot
sudo systemctl reboot. Users wishing to power down a system can
sudo systemctl halt.
Use the NOPASSWD: tag only if you do not want to be prompted for your password.
In my case, the exact line is:
anmol ALL=NOPASSWD: /bin/systemctl hibernate
(Note that the location of
systemctl might be different on your system.)
After this, you can write
sudo systemctl hibernate fron cron to hibernate.
Note: Directly modifying
/etc/sudoers is bad; instead make a custom sudoers file under
/etc/sudoers.d/ using the command -
sudo visudo -f /etc/sudoers.d/custom.