Cron not playing sound from script

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

mohittomar13

Mohit Tomar
Thread Starter
Joined
May 2, 2011
Messages
603
I got a script from the net and made some modifications to it so that I can get alerts when battery percentage falls under 15% charge. The script works perfectly when I run it from terminal and also plays sound but when I runt it using corn scheduling it doesn't play the sound. I'm including the script and the log.

Script
Bash:
#! /bin/bash

(
   # Read battery percentage value and extract integer value upto 2 places
   BATT_VAL=$(upower -i /org/freedesktop/UPower/devices/battery_BAT1 | grep percentage | cut -c 26-27)
 
   # Sends a notification if power is under 15% and machine is not on AC power
   if ! on_ac_power; then
        if (($BATT_VAL < "15")); then
            eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME xfce4-session)/environ)";
            notify-send 'BATTERY LOW' "Battery is less than ${BATT_VAL}%. Connect a charger now" --icon="/usr/share/icons/gnome/32x32/actions/gtk-cancel.png"
            paplay /usr/share/sounds/freedesktop/stereo/complete.oga
        fi
   fi
 
   # Sends a notification if battery level is equal to or over 90% and is on AC power
   if on_ac_power; then
        if (($BATT_VAL  >= "90")); then
            eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME xfce4-session)/environ)";
            notify-send 'BATTERY FULL' "Battery charged! You can now unplug your laptop!" --icon="/usr/share/icons/Humanity-Dark/status/16/gpm-battery-charged.svg"
            paplay /usr/share/sounds/freedesktop/stereo/complete.oga
        fi
   fi
) >& ~/.cronlog/batt.log
Log
Bash:
Connection failure: Connection refused
pa_context_connect() failed: Connection refused
I know the reason behind why it doesn't work, which is Environment Variables, but I don't know how to fix it.

Entry in the crontab is
*/3 * * * * /bin/bash /home/mohit/bin/battery-alert-net.sh

OS= Dual boot Xubuntu 20.4 LTS and Windows 10 (Pre-installed)
Machine= Acer Aspire 5 515


Thanks(y)
 

peterh40

Peter
Joined
Apr 15, 2007
Messages
1,496
In any scripts, make sure that you give the full pathname to any executable files. So instead of just 'paplay', enter /usr/bin/paplay (run 'which paplay' to view pathname).
 

mohittomar13

Mohit Tomar
Thread Starter
Joined
May 2, 2011
Messages
603
In any scripts, make sure that you give the full pathname to any executable files. So instead of just 'paplay', enter /usr/bin/paplay (run 'which paplay' to view pathname).
Thanks for your reply. However, I don't think it is because of incomplete paths. As I've logged the error generated by the script. It shows some error in connection establishment. The connection is refused to the underlying pulse layer (or something similar). Funny thing is that it shows that same error when I login using the root account and try to use paplay in the terminal while logged in as root user. Paplay doesn't seem to work in the terminal itself with the root account while it has no problem when i run it using my own account. Like i said it has to do something with the environment variables. I will try your suggestion though and let you know if ot works.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top