简体   繁体   中英

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

Set your $DISPLAY variable before calling Zenity.

export DISPLAY=:0   

Understanding linux DISPLAY variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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