crontab在我的Mac上不执行'ioreg' [英] crontab doesn't execute 'ioreg' on my mac

查看:336
本文介绍了crontab在我的Mac上不执行'ioreg'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Mac上跟踪电池信息, 因此,我将脚本文件分配给crontab,但是它不起作用.

I would like track my battery information on my mac, So I assign a script file to crontab but it doesn't work.

#!/bin/bash
#getbattery.sh
CURRENT_CAPACITY=$(ioreg -l -n AppleSmartBattery -r | grep CurrentCapacity | awk '{print $3}')
MAX_CAPACITY=$(ioreg -l -n AppleSmartBattery -r | grep MaxCapacity | awk '{print $3}')
CHARGE=$(echo $CURRENT_CAPACITY $MAX_CAPACITY | awk '{printf ("%i", $1/$2 * 100)}')
echo "$CHARGE""%  $(date) "

-

#my crontab content:
*/1 * * * * ~/getbattery.sh >> ~/batteryinfo.txt

为什么 ioreg 在crontab中不起作用? 请告诉我,如果有人知道我的问题会怎样.

Why ioreg doesn't work in the crontab? Please tell me what happen if anybody knows my problem.

谢谢.

推荐答案

cron作业在非常小的环境下运行,其中包括非常基本的PATH(仅/usr/bin:/bin).但是ioreg在/usr/sbin中,因此它不会作为基于该PATH的命令被找到.共有三种简单的解决方案:

cron jobs run with a very minimal environment, including a very basic PATH (just /usr/bin:/bin). But ioreg is in /usr/sbin, so it won't be found as a command based on that PATH. There are three easy solutions:

  1. 在crontab中设置PATH:

  1. Set the PATH in your crontab:

PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
*/1 * * * * ~/getbattery.sh >> ~/batteryinfo.txt

  • 在脚本中设置PATH:

  • Set the PATH in your script:

    #!/bin/bash
    #getbattery.sh
    PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
    CURRENT_CAPACITY=$(ioreg -l -n AppleSmartBattery -r | grep CurrentCapacity | awk '{print $3}')
    # etc...
    

  • 在脚本中为ioreg(以及/bin或/usr/bin中没有的任何其他命令)使用显式路径:

  • Use an explicit path for ioreg (and any other commands not in /bin or /usr/bin) in your script :

    #!/bin/bash
    #getbattery.sh
    CURRENT_CAPACITY=$(/usr/sbin/ioreg -l -n AppleSmartBattery -r | grep CurrentCapacity | awk '{print $3}')
    # etc...
    

  • 这篇关于crontab在我的Mac上不执行'ioreg'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆