3

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
  • 197,895
  • 55
  • 485
  • 740

3 Answers3

5

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
  • 48,105
  • 1
    The first 2 steps can be replaced by 1 - sudo crontab -e. And systemctl works too this way. – Anmol Singh Jaggi Jun 11 '16 at 07:25
  • Yep I just wanted to confirm root level. systemctl I agree seems to be the future tool. I will look if there another using it at user level – user.dz Jun 11 '16 at 07:28
  • You didn't answer why my current method isn't working. systemctl hibernate is working without root when run manually. Then, why is it not running from cron ? – Anmol Singh Jaggi Jun 11 '16 at 07:30
  • Really!!! It does not work for me only with root power (sudo). But think I have seen a question here before about similar thing related to auto shutdown using DBus., the command should be launch from terminal (tty) with the user already passed the login. – user.dz Jun 11 '16 at 07:39
  • Oh wow! What Ubuntu version are you using ? – Anmol Singh Jaggi Jun 11 '16 at 07:40
  • Ubuntu 16.04 LTS as yours but installed on VBox (I didn't want to reboot may machine) it just keep waiting till I cancel ctrl+c, In the real machine too 16.04 (upgraded from 15.10) I got a GUI dialog to enter my passwd if I cancel it I got same messages you mention in the question – user.dz Jun 11 '16 at 07:56
  • Sadly, notify-send isn't working on root cron. Do you know any solution to that ? – Anmol Singh Jaggi Jun 11 '16 at 11:00
  • @AnmolSinghJaggi, yes here is https://askubuntu.com/questions/638867/alert-the-user-when-they-unsafely-unplug-an-usb-thumb-drive/ – user.dz Jun 11 '16 at 11:11
  • I am sorry, but how does that link help ? – Anmol Singh Jaggi Jun 11 '16 at 11:24
  • @AnmolSinghJaggi sorry for such long question, here directly the wanted command /usr/bin/sudo -u username DISPLAY=:0 notify-send 'Unsafe Remove' '<b><i>Your long message</b></i>' -i /usr/share/icons/gnome/48x48/emotes/face-worried.png -t 10000 , you may find some differences on other answer. if you want to see search here for udev notify-send , acpi notify-send & cron notify-send – user.dz Jun 11 '16 at 11:35
  • 1
    Like user.dz said, add hibernate to root's crontab, but I would use a slightly different command to launch crontab: sudo crontab -e -u root From man crontab : "Note that su(8) can confuse crontab and that if you are running inside of su(8) you should always use the -u option for safety's sake." I don't know if crontab has the same problem with sudo that it has with su, but, may as well play it safe. (I would have added this as a comment to user.dz answer, but, I don't have the rep.) – SideShowFry Feb 02 '17 at 19:19
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.

  • 1
    Great, here is another reference for polkit option from Ubuntu help pages. Possible to use Identity=unix-user:username to give permission to one user only same as the sudoer solution. – user.dz Jun 13 '16 at 05:16
  • I found this worked for me when the command is sudo /bin/systemctl hibernate. – hwong557 Sep 16 '18 at 14:52
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,426
rhuanpk
  • 101
  • 3