使用 systemd 计时器代替 cron

使用 systemd 计时器代替 cron

我在 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:

应用解决方案后亚历山大·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

答案1

这个问题的答案是,User=nobody不是与交换User=ziga ,而是与User=root交换/etc/systemd/system/battery.service。不知何故,即使用户ziga拥有使用命令的所有权限,sudo它也无法systemctl hibernate在 bash 脚本内部执行。我真的不知道为什么会发生这种情况。所以工作文件如下:

/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=zigaor一起使用User=nobody,但我们需要在最后一个脚本中更改/usr/bin/systemctl hibernate为。sudo /usr/bin/systemctl hibernate所以它看起来好像User变量在某种程度上并不重要...哦,你也可以从最后一个脚本中删除绝对名称并将第一行从 更改#!/bin/sh#!/bin/bash。我也改为WantedBy=timers.targetWantedBy=battery.service/etc/systemd/system/battery.timer

就这样吧。低电量休眠笔记本电脑的最佳 cron 替代品。 =)

答案2

也许您应该尝试添加启动后首次启动,如下所示:

[Timer] OnBootSec=15min OnUnitActiveSec=2m

相关内容