4

I am trying to hibernate from cron using the command systemctl hibernate. However, I am getting the following error:

Failed to set wall message, ignoring: Interactive authentication required.
Failed to hibernate system via logind: Interactive authentication required.
Failed to start hibernate.target: Interactive authentication required.
See system logs and 'systemctl status hibernate.target' for details.

If I execute the above command manually from terminal, it works as expected.

How do I hibernate from cron?

I am using Ubuntu 16.04.

muru
  • 207,228

3 Answers3

6

This is happening because it needs root privileges.
The solution is to add the hibernate command using sudo crontab -e -u root instead of crontab -e.

polkit is necessary for power management as an unprivileged user. If you are in a local systemd-logind user session and no other session is active, the following commands will work without root privileges. If not (for example, because another user is logged into a tty), systemd will automatically ask you for the root password.

Power management commands:

systemctl reboot|poweroff|suspend|hibernate|hybrid-sleep

Reference: https://wiki.archlinux.org/index.php/Systemd#Power_management

user.dz
  • 49,176
3

The other answer is great! But it requires root cron.

If you want to hibernate from non-sudo cron, there are 2 options:

1. Using polkit

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.

2. Using visudo

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.

0

In Debian SID this worked for me:

  1. In /etc/polkit-1/rules.d/cron-suspend.rules file add:

    polkit.addRule(function(action, subject) {
         if (subject.user == "my-user" && (action.id == "org.freedesktop.login1.suspend" || action.id == "org.freedesktop.login1.suspend-multiple-session")) {
             return polkit.Result.YES;
         }
    });
    
  2. Restart the service:

    systemctl restart polkit
    

OBS: change suspend to hibernate if needed.

zx485
  • 2,865
rhuanpk
  • 111
  • 5