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]
Identity=unix-user:*
Action=org.freedesktop.login1.hibernate;org.freedesktop.login1.hibernate-multiple-sessions
ResultAny=yes
named com.0.enable-hibernation-from-cron.pkla
in the directory /etc/polkit-1/localauthority/50-local.d/
.
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 visudo
command.
user hostname =NOPASSWD: /usr/bin/systemctl poweroff,/usr/bin/systemctl halt,/usr/bin/systemctl reboot
Substitute user
for your username and hostname
for the machine's hostname.
Now your user can shutdown with sudo systemctl poweroff
, and reboot
with sudo systemctl reboot
. Users wishing to power down a system can
also use 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
.
pm-hibernate
?sudo
to run.sudo
when test using your user. but systemcron
already running as root (only if you are trying to use user's cron)sudo crontab -e
, both of the commands are working. But, is there any way to do it from normal crontab itself ? I guess this will do for thepm-hibernate
command. But isn'tsystemctl
better thanpm-utils
(source) ?