11223342124
11223342124

Reputation: 175

Cron job doesn't execute the shell script

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

Upvotes: 0

Views: 188

Answers (1)

pacholik
pacholik

Reputation: 8972

Set your $DISPLAY variable before calling Zenity.

export DISPLAY=:0   

Understanding linux DISPLAY variable

Upvotes: 1

Related Questions