简体   繁体   English

CRON作业和bash脚本以检查电池状态和蜂鸣声

[英]CRON job and bash script to check battery state and beep

I recently changed desktop environments and the new one doesn't have a battery monitor. 我最近更改了桌面环境,而新的环境没有电池监视器。 I had managed to create a simple manual one by doing the following: 通过执行以下操作,我设法创建了一个简单的手册:

alias bat='upower -i /org/freedesktop/UPower/devices/battery_BAT0| grep -E "state|to\ full|percentage"'

I would like to take this to the next level and create a script that not only checks the battery level but also beeps according to it, lets say it will beep if the battery percent is less then 20%. 我想将其带入一个新的水平,并创建一个脚本,该脚本不仅检查电池电量,还根据它发出蜂鸣声,可以说如果电池百分比小于20%,它将发出蜂鸣声。 I would then like to run it as CRON job, which lets say will run every 5 minutes. 然后,我想将其作为CRON作业运行,可以说每5分钟运行一次。

I also used the following to create a beep sound which I liked: 我还使用以下内容创建了我喜欢的蜂鸣声:

alias beep='paplay /usr/share/sounds/ubuntu/stereo/message-new-instant.ogg'

Assuming the CRON part is covered, how would you write this script in bash, assuming you use the same command as used in 'bat': 假设已涵盖CRON部分,假设使用与'bat'中使用的命令相同的命令,您将如何用bash编写此脚本:

upower -i /org/freedesktop/UPower/devices/battery_BAT0

thanks 谢谢

I came up with the following solution: 我想出了以下解决方案:

1) Create a bash script: 1)创建一个bash脚本:

#!/bin/bash

bat='upower -i /org/freedesktop/UPower/devices/battery_BAT0| grep -E "state|to\ full|percentage"'
beep='paplay /usr/share/sounds/ubuntu/stereo/message-new-instant.ogg'

# This gets the integer percentage of current battery capacity
p=$(eval $bat | grep 'percentage' | awk '{print $2}' | awk -F '.' '{print $1}')

# if bat < 25% -> BEEP once
if [ "$p" -le 25 ] ; then
    eval $beep
fi

2) Set the permissions: 2)设置权限:

chmod 700 /path/to/script.sh

3) To set a cron job run crontab -e and add the following line: 3)要设置cron作业,请运行crontab -e并添加以下行:

*/1 * * * * /path/to/script.sh

which means the script will be run every minute. 这意味着脚本将每分钟运行一次。

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

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