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.

  • How about pm-hibernate? – user.dz Jun 11 '16 at 3:22
  • That requires sudo to run. – Anmol Singh Jaggi Jun 11 '16 at 5:47
  • Yep, It need root access, So you need sudo 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 5:58
  • 1
    If I schedule it with 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 the pm-hibernate command. But isn't systemctl better than pm-utils (source) ? – Anmol Singh Jaggi Jun 11 '16 at 7:16
  • 1
    Related question. – Anmol Singh Jaggi Jun 11 '16 at 7:53
3

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

  • 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 7: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 7: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 7: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 7:39
  • 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 2 '17 at 19:19
2

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 5:16
  • I found this worked for me when the command is sudo /bin/systemctl hibernate. – hwong557 Sep 16 '18 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.