0

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:

  1. when battery state is charging and it is around 80% full, it reminds me to unplug the cable.

  2. 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:

  1. script works when it is executed independently of the cron
  2. cron job works - I have tested it by appending date command response to a file every minute
  3. I have tried without declaring RESPONSE variable, and it still doesn't work
11223342124
  • 175
  • 11
  • 1
    Is that a legal `crontab` file on your OS? Also, this question is likely off-topic as it's about `crontab` configuration and is likely better on unix.stackexchange.com. – Andrew Henle Sep 25 '18 at 13:14
  • @AndrewHenle yes, its legal, I omitted rest of the file. and thanks for downvote (if thats you). – 11223342124 Sep 25 '18 at 13:18
  • Is your script executable? is the crontab user the same as has permissions? does crontab work for other things, like `date>/tmp/proof`? – Paul Hodges Sep 25 '18 at 14:40
  • @PaulHodges 'Is your script executable' -> Yes, it is. 'does crontab work for other things, like date>/tmp/proof? ' -> it does, I wrote details in question – 11223342124 Sep 25 '18 at 18:24
  • Is there a log? – Paul Hodges Sep 26 '18 at 14:34

1 Answers1

1

Set your $DISPLAY variable before calling Zenity.

export DISPLAY=:0   

Understanding linux DISPLAY variable

pacholik
  • 8,607
  • 9
  • 43
  • 55