I have a script that verifies the battery level using acpi and if it's below certain threshold it should lock the machine and hibernate. The script is executed every minute using crontab
The problem is that the machine gets locked but never hibernates.
The script:
#!/bin/sh
acpi -b | awk -F'[,:%]' '{print $2, $3}' | {
read -r status capacity
if [ "$status" = Discharging -a "$capacity" -lt 10 ]; then
echo 'Success' >> /tmp/low;
logger "Critical battery threshold";
DISPLAY=:0 i3lock -t -i $(ls -d ~/.wallpapers/* | shuf | head -n 1);
echo 'Locked' >> /tmp/low;
systemctl hibernate;
fi
}
The /tmp/low log file shows the following:
$ cat /tmp/low
Success
Locked
Success
Locked
Success
Locked
I tried to directly run a similar script (Without the ACPI check) and it worked perfectly
The testing script:
#!/bin/sh
acpi -b | awk -F'[,:%]' '{print $2, $3}' | {
read -r status capacity
echo 'Success' >> /tmp/low;
logger "Critical battery threshold";
DISPLAY=:0 i3lock -t -i $(ls -d ~/.wallpapers/* | shuf | head -n 1);
echo 'Locked' >> /tmp/low;
systemctl hibernate;
}
Same testing script was run using at but it didn't hibernate the machine. Any ideas why crontab can't execute systemctl hibernate?
systemctlcommand takes two parameters: the COMMAND (i.e., 'start', 'stop', 'status', etc.) and the NAME of the service to execute the COMMAND on. In your test instance , it's missing the COMMAND part of that setup.systemctl hibernatefrom terminal works perfectly fine. The script also works fine, except when tools such ascronoratruns it