Reputation: 175
I want to create a shell script which will be run every minute by cron job. Script's job will be to notify me to (un)plug the charger in different conditions:
when battery state is charging and it is around 80% full, it reminds me to unplug the cable.
when battery state is discharging and it is around 40% full, it reminds me to plug the cable back.
My crontab file:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * /home/aleksa/charge.sh
# newline
My /home/aleksa/charge.sh
script:
#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
RESPONSE="$(upower -i /org/freedesktop/UPower/devices/battery_BAT0)"
#if percentage is around 40%, and state is discharging
if echo "${RESPONSE}" | grep -E '38%|39%|40%|41%|42%' && echo "${RESPONSE}" | grep -w 'discharging'
then zenity --notification --window-icon=update.png --text "Connect charger";
fi
#if percentage is around 80%, and state is charging
if echo "${RESPONSE}" | grep -E '78%|79%|80%|81%|82%' && echo "${RESPONSE}" | grep -w 'charging'
then zenity --notification --window-icon=update.png --text "Disconnect charger";
fi
I think that problem is in the script because I have tried different things in order to detect where the problem is:
date
command response to a file every minuteRESPONSE
variable, and it still doesn't workUpvotes: 0
Views: 188
Reputation: 8972
Set your $DISPLAY variable before calling Zenity.
export DISPLAY=:0
Understanding linux DISPLAY variable
Upvotes: 1