I have a command like this:

if [ $battery_level -le 6 -a $STATUS = $NOT_CHARGING ] #Battery Low 1
/usr/bin/notify-send -i "$ICONL" "Battery critically low!" "Battery level is ${battery_level}%!"
paplay /usr/share/sounds/freedesktop/stereo/dialog-information.oga

It gives a message and a sound at a certain battery level. But it repeats too often.

I can set it to sleep and run again by separating the same command with a line like sleep 120s, but I do not know exactly how many times should it be run.

I need it to run at an interval of two minutes, indefinitely (as at some point system sleep or hibernation is triggered by a completely different script).

Looking here: How to Run or Repeat a Linux Command Every X Seconds Forever, I see that an option is sleep and the other is watch.

watch seems to work but it only works with the form

watch -n 120 <my_line>, and that changes a bit the output (battery level is not shown) anymore.

  • 2
    Use while in place of if and place the whole code you show in file+make it executable. And then, just watch -n 120 your_battery_level_indicator.sh
    – user218374
    Apr 1, 2017 at 18:22

1 Answer 1


Just use a while loop:

while [ "$battery_level" -le 6 ] && [ "$STATUS" = "$NOT_CHARGING" ]
  • but then will it give any message if the level goes below 6?
    – user32012
    Apr 1, 2017 at 18:08
  • What do you mean? If you mean how does it update battery_level, simply replace it with the command you used to get battery_level
    – l0b0
    Apr 1, 2017 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy