Linux

使用 systemd 計時器而不是 cron

  • June 28, 2016

我在 Arch Linux 上,我正在嘗試創建一個 systemd 計時器作為 cron 替代方案,用於在電池電量不足的情況下使筆記型電腦休眠。所以我寫了這三個文件:

/etc/systemd/system/battery.service

[Unit]
Description=Preko skripte preveri stanje baterije in hibernira v kolikor je stanje prenizko

[Service]
Type=oneshot
ExecStart=/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescript
User=nobody
Group=systemd-journal

/etc/systemd/system/battery.timer

[Unit]
Description=Periodical checking of battery status every two minutes

[Timer]
OnUnitActiveSec=2min 

[Install]
WantedBy=timers.target

/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescript

#!/bin/sh
/usr/bin/acpi -b | /usr/bin/awk -F'[,:%]' '{print $2, $3}' | (
       read -r status capacity
       if [ "$status" = Discharging ] && [ "$capacity" -lt 50 ]; then
               /usr/bin/systemctl hibernate
       fi                                                                                                                
)    

然後啟用計時器我執行:

sudo systemctl enable battery.timer
sudo systemctl start battery.timer

而且不知何故它不起作用。腳本獨立工作。這意味著如果我執行下面的命令,我的電腦就可以休眠了。

/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescript

添加1:

在啟用和啟動計時器後,我執行了一些檢查,這就是我得到的:

[ziga@ziga-laptop ~]$ systemctl list-timers
NEXT                          LEFT       LAST                          PASSED    UNIT                         ACTIVATES
n/a                           n/a        n/a                           n/a       battery.timer                battery.serv
Tue 2016-06-28 00:00:00 CEST  42min left Mon 2016-06-27 00:01:54 CEST  23h ago   logrotate.timer              logrotate.se
Tue 2016-06-28 00:00:00 CEST  42min left Mon 2016-06-27 00:01:54 CEST  23h ago   shadow.timer                 shadow.servi
Tue 2016-06-28 00:00:00 CEST  42min left Mon 2016-06-27 00:01:54 CEST  23h ago   updatedb.timer               updatedb.ser
Tue 2016-06-28 22:53:58 CEST  23h left   Mon 2016-06-27 22:53:58 CEST  23min ago systemd-tmpfiles-clean.timer systemd-tmpf

[ziga@ziga-laptop ~]$ systemctl | grep battery
battery.timer  loaded active elapsed   Periodical checking of battery status every two minutes

添加2:

應用Alexander T的解決方案後,我的計時器啟動(檢查下面的程式碼),但腳本不會使我的筆記型電腦休眠,而如果我直接執行它會使其休眠。

[ziga@ziga-laptop ~]$ systemctl list-timers
NEXT                          LEFT          LAST                          PASSED       UNIT                         ACTIVATES
Tue 2016-06-28 19:17:30 CEST  1min 43s left Tue 2016-06-28 19:15:30 CEST  16s ago      battery.timer                battery.service

這個問題的答案是不交換,User=nobody而是User=ziga 交換**/etc/systemd/system/battery.service**。不知何故,即使使用者擁有使用命令的所有權限,它也無法在 bash 腳本中執行。我真的不知道為什麼會這樣。所以工作文件如下:User=root``ziga``sudo``systemctl hibernate

/etc/systemd/system/battery.service

[Unit]
Description=Preko skripte preveri stanje baterije in hibernira v kolikor je stanje prenizko

[Service]
Type=oneshot
ExecStart=/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescript
User=root
Group=systemd-journal

/etc/systemd/system/battery.timer

[Unit]
Description=Periodical checking of battery status every two minutes

[Timer]
OnBootSec=2min
OnUnitActiveSec=2min 

[Install]
WantedBy=battery.service

/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescript

#!/bin/sh
/usr/bin/acpi -b | /usr/bin/awk -F'[,:%]' '{print $2, $3}' | (
       read -r status capacity
       if [ "$status" = Discharging ] && [ "$capacity" -lt 7 ]; then
               /usr/bin/systemctl hibernate
       fi
)

我試過了,它也適用於User=zigaorUser=nobody但我們需要在最後一個腳本中/usr/bin/systemctl hibernate更改sudo /usr/bin/systemctl hibernate。所以看起來User變數不知何故甚至無關緊要……哦,你也可以從最後一個腳本中刪除絕對名稱並將第一行從更改#!/bin/sh#!/bin/bash. 我也更改WantedBy=timers.targetWantedBy=battery.servicein /etc/systemd/system/battery.timer

你去吧。在電池電量不足的情況下休眠筆記型電腦的最佳 cron 替代方案。=)

引用自:https://unix.stackexchange.com/questions/292444