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
? – user.dz Jun 11 '16 at 03:22sudo
to run. – Anmol Singh Jaggi Jun 11 '16 at 05:47sudo
when test using your user. but systemcron
already running as root (only if you are trying to use user's cron) – user.dz Jun 11 '16 at 05:58sudo
, I get the errorThis utility may only be run by the root user.
. Withsudo
, I get the error -sudo: no tty present and no askpass program specified
. – Anmol Singh Jaggi Jun 11 '16 at 07:05sudo 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) ? – Anmol Singh Jaggi Jun 11 '16 at 07:16