cron - 一个脚本有效,另一个脚本无效

时间:2016-06-21 00:36:17

标签: bash cron archlinux systemd

我在Archlinux上,我试图强制一个cron应用程序来执行我的脚本。所以我安装了包fcron并首先运行它的守护进程然后我添加了fcrontab,如下所示:

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

好的,这就是它变得棘手的地方。如果脚本的内容如下所示,则执行此脚本:

#!/bin/bash
PATH=/usr/bin
touch ~/test-002.txt

但如果内容如下所示,它将无法执行:

#!/bin/bash
PATH=/usr/bin
acpi -b | awk -F'[,:%]' '{print $2, $3}' | {
    read -r status capacity
    if [ "$status" = Discharging -a "$capacity" -lt 5 ]; then
        logger "Critical battery threshold"
        systemctl hibernate
    fi
}

为什么?即使Archlinux Wiki说它应该有用,但我认为这是错误的 - 我只更改了#!/bin/bash并添加了PATH=/usr/bin脚本独立工作......

修改

所以我编辑了我的脚本,如下所示,它不起作用。

#!/bin/sh
SHELL=/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
)

如果我跑

[ziga@ziga-laptop hibernate]$ run-parts ~/Dropbox/workspace/operacijski/archlinux/hibernate/ -v
run-parts: executing /home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate//hibernatescript

我的电脑处于休眠状态,但cron并没有使用完全相同的脚本来休眠我的电脑。我也尝试过fcron但它虽然守护进程完全运行并且设置了fcrontab但它仍然无效。这适用于任何更简单的脚本,而它不适用于这个。

以下是一些信息:

[ziga@ziga-laptop ~]$ systemctl status fcron.service     
● fcron.service - fcron periodical command scheduler
   Loaded: loaded (/usr/lib/systemd/system/fcron.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2016-06-21 10:39:48 CEST; 58min ago
  Process: 419 ExecStart=/usr/bin/fcron (code=exited, status=0/SUCCESS)
 Main PID: 423 (fcron)
    Tasks: 1 (limit: 512)
   CGroup: /system.slice/fcron.service
           └─423 /usr/bin/fcron

Jun 21 11:34:00 ziga-laptop fcron[12833]: Job '/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescrip
Jun 21 11:34:02 ziga-laptop fcron[12833]: Job '/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescrip
Jun 21 11:35:00 ziga-laptop fcron[13021]: Job '/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescrip
Jun 21 11:35:02 ziga-laptop fcron[13021]: Job '/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescrip
Jun 21 11:36:00 ziga-laptop fcron[13210]: Job '/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescrip
Jun 21 11:36:02 ziga-laptop fcron[13210]: Job '/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescrip
Jun 21 11:37:00 ziga-laptop fcron[13407]: Job '/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescrip
Jun 21 11:37:02 ziga-laptop fcron[13407]: Job '/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescrip
Jun 21 11:38:00 ziga-laptop fcron[13599]: Job '/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescrip
Jun 21 11:38:02 ziga-laptop fcron[13599]: Job '/home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescrip
[ziga@ziga-laptop ~]$ fcrontab -l
2016-06-21 11:38:48  INFO listing ziga's fcrontab
* * * * * /home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescript
[ziga@ziga-laptop ~]$ cat /home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/hibernatescript
#!/bin/sh
SHELL=/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
)
[ziga@ziga-laptop ~]$ ls -l /home/ziga/Dropbox/workspace/operacijski/archlinux/hibernate/
total 16
-rwxrwxrwx 1 ziga users  214 Jun 21 08:46 hibernatescript
-rw-rw-rw- 1 ziga users 2832 Jun 21 00:24 README.txt
[ziga@ziga-laptop ~]$ groups ziga
lp audio bumblebee users
[ziga@ziga-laptop ~]$ 

0 个答案:

没有答案