简体   繁体   English

crontab在我的Mac上不执行'ioreg'

[英]crontab doesn't execute 'ioreg' on my mac

I would like track my battery information on my mac, So I assign a script file to crontab but it doesn't work. 我想在Mac上跟踪电池信息,因此我将脚本文件分配给了crontab,但是它不起作用。

#!/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

Why ioreg doesn't work in the crontab? 为什么ioreg在crontab中不起作用? Please tell me what happen if anybody knows my problem. 如果有人知道我的问题,请告诉我会发生什么。

thanks. 谢谢。

cron jobs run with a very minimal environment, including a very basic PATH (just /usr/bin:/bin). cron作业在一个非常小的环境下运行,包括一个非常基本的PATH(仅/ usr / bin:/ bin)。 But ioreg is in /usr/sbin, so it won't be found as a command based on that PATH. 但是ioreg位于/ usr / sbin中,因此它不会作为基于该PATH的命令被找到。 There are three easy solutions: 有三种简单的解决方案:

  1. Set the PATH in your crontab: 在crontab中设置PATH:

     PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin */1 * * * * ~/getbattery.sh >> ~/batteryinfo.txt 
  2. Set the PATH in your script: 在脚本中设置PATH:

     #!/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... 
  3. Use an explicit path for ioreg (and any other commands not in /bin or /usr/bin) in your script : 在脚本中使用ioreg的显式路径(以及/ bin或/ usr / bin中的其他命令):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM