Prevent suspend while audio/music playing. - Solved

Please post suggestions for improvement of Cinnamon on:
https://github.com/linuxmint/Cinnamon
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
jaygambrel

Prevent suspend while audio/music playing. - Solved

Post by jaygambrel »

I have been looking for a reliable way to prevent Linux Mint 19 - Cinnamon from suspending while I am listening to music. I want the machine to suspend after the music finishes playing. Caffeine only works for applications running full screen so it does not do what I need it to do. I have found a few potential links that were helpful:

https://superuser.com/questions/393448/ ... ash-script
https://ubuntuforums.org/showthread.php?t=2232064

I used these to write a very basic script that that will change the suspend timeout in Cinnamon's power settings using gsettings to "Never" temporarily while music is playing and then revert when the music stops. This is my first real attempt at a script so if you can improve on it feel free. It will likely only work in Cinnamon though.

Code: Select all

#!/bin/sh

# Script to temporarily set Cinnamon's suspend timout for AC and battery to "Never"
# while audio is playing.  It then reverts the settings when audio is no longer detected.

# Create directory to store settings if it doesn't exist.
if [ ! -d ~/.config/audiocaffeine ]; then
    echo "Configuration directory not found!"
    echo "Creating ~/.config/audiocaffeine"
    mkdir ~/.config/audiocaffeine
fi

# Restore previous value for AC suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/acsuspend ]; then
    echo "Restoring previous AC suspend timeout."
    read acsuspendtime < ~/.config/audiocaffeine/acsuspend
    gsettings set org.cinnamon.settings-daemon.plugins.power sleep-inactive-ac-timeout $acsuspendtime
    echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
    rm ~/.config/audiocaffeine/acsuspend
fi

# Restore previous value for battery suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/battsuspend ]; then
    echo "Restoring previous battery suspend timeout."
    read battsuspendtime < ~/.config/audiocaffeine/battsuspend
    gsettings set org.cinnamon.settings-daemon.plugins.power sleep-inactive-battery-timeout $battsuspendtime
    echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
    rm ~/.config/audiocaffeine/battsuspend
fi

# Start main loop to check if audio is playing

while true; do

    # Use pactl to detect if there are any running audio sources.
    if pactl list | grep -q RUNNING; then

        echo "Audio detected."

        # If AC timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Saving current AC suspend timeout."
            gsettings get org.cinnamon.settings-daemon.plugins.power sleep-inactive-ac-timeout > ~/.config/audiocaffeine/acsuspend
        fi

        # If battery timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Saving current battery suspend timeout."
            gsettings get org.cinnamon.settings-daemon.plugins.power sleep-inactive-battery-timeout > ~/.config/audiocaffeine/battsuspend
        fi

        # Set the suspend timouts to Never using gsettings.
        echo "Changing suspend timeouts."
        gsettings set org.cinnamon.settings-daemon.plugins.power sleep-inactive-ac-timeout 0
        gsettings set org.cinnamon.settings-daemon.plugins.power sleep-inactive-battery-timeout 0

    else
        echo "No audio detected."
    
        # Restore previous value for AC suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Restoring previous AC suspend timeout."
            read acsuspendtime < ~/.config/audiocaffeine/acsuspend
            gsettings set org.cinnamon.settings-daemon.plugins.power sleep-inactive-ac-timeout $acsuspendtime
            echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
            rm ~/.config/audiocaffeine/acsuspend
        fi

        # Restore previous value for battery suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Restoring previous battery suspend timeout."
            read battsuspendtime < ~/.config/audiocaffeine/battsuspend
            gsettings set org.cinnamon.settings-daemon.plugins.power sleep-inactive-battery-timeout $battsuspendtime
            echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
            rm ~/.config/audiocaffeine/battsuspend
        fi

    fi

    # Pause the script for 60 seconds before doing the loop again.
    sleep 60s

done
It seems to work for me so far. If anyone has any suggestions to make it more reliable/efficient please feel free to post modifications. If anyone would like to modify it to work with Mate for those using the Mate edition, feel free.

Jay
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 3 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
jaygambrel

Re: Prevent suspend while audio playing.

Post by jaygambrel »

I have done a little more work on the above script and it should now work in Mate as well as Ubuntu running Gnome. I have tried it out on Live USB versions of Linux Mint 19 Cinnamon and Mate as well as Ubuntu 18.04 - Gnome. I hope this comes in helpful for a few other people.

Code: Select all

#!/bin/sh

# Audiocaffeine - Pulseaudio

# Works in Linux Mint 19 - Cinnamon, Linux Mint 19 - Mate, Ubuntu 18.04 - Gnome

# Script to temporarily set suspend timout for AC and battery to "Never"
# while audio is playing.  It then reverts the settings when audio is no longer detected.

# Determine if a valid desktop environment is running and exit if it doesn't.
echo "Reported desktop environment: ""$XDG_CURRENT_DESKTOP"
if [ "$XDG_CURRENT_DESKTOP" = "X-Cinnamon" ]; then
    actimeoutid="org.cinnamon.settings-daemon.plugins.power sleep-inactive-ac-timeout"
    batttimeoutid="org.cinnamon.settings-daemon.plugins.power sleep-inactive-battery-timeout"
    disablevalue=0
elif [ "$XDG_CURRENT_DESKTOP" = "MATE" ]; then
    actimeoutid="org.mate.power-manager sleep-computer-ac"
    batttimeoutid="org.mate.power-manager sleep-computer-battery"
    disablevalue=0
elif [ "$XDG_CURRENT_DESKTOP" = "ubuntu:GNOME" ]; then
    actimeoutid="org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type"
    batttimeoutid="org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type"
    disablevalue="nothing"
else
    echo "No valid desktop environment is running"
    exit 1
fi

# Create .config directory to store settings if it doesn't exist.
if [ ! -d ~/.config ]; then
    echo ".config directory not found!"
    echo "Creating ~/.config"
    mkdir ~/.config
fi

# Create audiocaffeine directory to store settings if it doesn't exist.
if [ ! -d ~/.config/audiocaffeine ]; then
    echo "Configuration directory not found!"
    echo "Creating ~/.config/audiocaffeine"
    mkdir ~/.config/audiocaffeine
fi

# Restore previous value for AC suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/acsuspend ]; then
    echo "Restoring previous AC suspend timeout."
    read acsuspendtime < ~/.config/audiocaffeine/acsuspend
    gsettings set $actimeoutid $acsuspendtime
    echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
    rm ~/.config/audiocaffeine/acsuspend
fi

# Restore previous value for battery suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/battsuspend ]; then
    echo "Restoring previous battery suspend timeout."
    read battsuspendtime < ~/.config/audiocaffeine/battsuspend
    gsettings set $batttimeoutid $battsuspendtime
    echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
    rm ~/.config/audiocaffeine/battsuspend
fi

# Start main loop to check if audio is playing

while true; do

    # Use pactl to detect if there are any running audio sources.
    if pactl list | grep -q "State: RUNNING"; then

        echo "Audio detected."

        # If AC timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Saving current AC suspend timeout."
            gsettings get $actimeoutid > ~/.config/audiocaffeine/acsuspend
        fi

        # If battery timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Saving current battery suspend timeout."
            gsettings get $batttimeoutid > ~/.config/audiocaffeine/battsuspend
        fi

        # Set the suspend timouts to Never using gsettings.
        echo "Changing suspend timeouts."
        gsettings set $actimeoutid $disablevalue
        gsettings set $batttimeoutid $disablevalue

    else
        echo "No audio detected."
    
        # Restore previous value for AC suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Restoring previous AC suspend timeout."
            read acsuspendtime < ~/.config/audiocaffeine/acsuspend
            gsettings set $actimeoutid $acsuspendtime
            echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
            rm ~/.config/audiocaffeine/acsuspend
        fi

        # Restore previous value for battery suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Restoring previous battery suspend timeout."
            read battsuspendtime < ~/.config/audiocaffeine/battsuspend
            gsettings set $batttimeoutid $battsuspendtime
            echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
            rm ~/.config/audiocaffeine/battsuspend
        fi

    fi

    # Pause the script for 60 seconds before doing the loop again.
    sleep 60s

done
Or here is a version that uses ALSA instead of Pulseaudio. The Pulseaudio version incorrectly reported sound playing when I opened the Sound Settings in System Settings and wouldn't stop until the System Settings application was closed. I haven't tested it as thoroughly though:

Code: Select all

#!/bin/sh

# Audiocaffeine - ALSA

# Works in Linux Mint 19 - Cinnamon, Linux Mint 19 - Mate, Ubuntu 18.04 - Gnome

# Script to temporarily set suspend timout for AC and battery to "Never"
# while audio is playing.  It then reverts the settings when audio is no longer detected.

# Determine if a valid desktop environment is running and exit if it doesn't.
    echo "Reported desktop environment: ""$XDG_CURRENT_DESKTOP"
if [ "$XDG_CURRENT_DESKTOP" = "X-Cinnamon" ]; then
    actimeoutid="org.cinnamon.settings-daemon.plugins.power sleep-inactive-ac-timeout"
    batttimeoutid="org.cinnamon.settings-daemon.plugins.power sleep-inactive-battery-timeout"
    disablevalue=0
elif [ "$XDG_CURRENT_DESKTOP" = "MATE" ]; then
    actimeoutid="org.mate.power-manager sleep-computer-ac"
    batttimeoutid="org.mate.power-manager sleep-computer-battery"
    disablevalue=0
elif [ "$XDG_CURRENT_DESKTOP" = "ubuntu:GNOME" ]; then
    actimeoutid="org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type"
    batttimeoutid="org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type"
    disablevalue="nothing"
else
    echo "No valid desktop environment is running"
    exit 1
fi

# Create .config directory to store settings if it doesn't exist.
if [ ! -d ~/.config ]; then
    echo ".config directory not found!"
    echo "Creating ~/.config"
    mkdir ~/.config
fi

# Create audiocaffeine directory to store settings if it doesn't exist.
if [ ! -d ~/.config/audiocaffeine ]; then
    echo "Configuration directory not found!"
    echo "Creating ~/.config/audiocaffeine"
    mkdir ~/.config/audiocaffeine
fi

# Restore previous value for AC suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/acsuspend ]; then
    echo "Restoring previous AC suspend timeout."
    read acsuspendtime < ~/.config/audiocaffeine/acsuspend
    gsettings set $actimeoutid $acsuspendtime
    echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
    rm ~/.config/audiocaffeine/acsuspend
fi

# Restore previous value for battery suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/battsuspend ]; then
    echo "Restoring previous battery suspend timeout."
    read battsuspendtime < ~/.config/audiocaffeine/battsuspend
    gsettings set $batttimeoutid $battsuspendtime
    echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
    rm ~/.config/audiocaffeine/battsuspend
fi

# Start main loop to check if audio is playing

while true; do

    # Use ALSA to detect if there are any running audio sources.
    if grep -q RUNNING /proc/asound/card*/*p/*/status 2>&1; then

        echo "Audio detected."

        # If AC timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Saving current AC suspend timeout."
            gsettings get $actimeoutid > ~/.config/audiocaffeine/acsuspend
        fi

        # If battery timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Saving current battery suspend timeout."
            gsettings get $batttimeoutid > ~/.config/audiocaffeine/battsuspend
        fi

        # Set the suspend timouts to Never using gsettings.
        echo "Changing suspend timeouts."
        gsettings set $actimeoutid $disablevalue
        gsettings set $batttimeoutid $disablevalue

    else
        echo "No audio detected."
    
        # Restore previous value for AC suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Restoring previous AC suspend timeout."
            read acsuspendtime < ~/.config/audiocaffeine/acsuspend
            gsettings set $actimeoutid $acsuspendtime
            echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
            rm ~/.config/audiocaffeine/acsuspend
        fi

        # Restore previous value for battery suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Restoring previous battery suspend timeout."
            read battsuspendtime < ~/.config/audiocaffeine/battsuspend
            gsettings set $batttimeoutid $battsuspendtime
            echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
            rm ~/.config/audiocaffeine/battsuspend
        fi

    fi

    # Pause the script for 60 seconds before doing the loop again.
    sleep 60s

done
I would appreciate any feedback that anyone would have to offer.

Jay
jaygambrel

Re: Prevent suspend while audio playing. - Solved

Post by jaygambrel »

How to install this script in Cinnamon:

1. Begin by opening the text editor as root and creating a blank file for the script. Open a terminal and type or paste the following command into it:

Code: Select all

sudo xed /usr/local/bin/audiocaffeine.sh

2. Enter your password when asked for it.

3. When the text editor opens, copy and paste the script from the Linux Mint forum into the text editor and select File > Save

4. Close the text editor.

5. Go back to or open a new terminal. Use the following command in the terminal to make the script executable:

Code: Select all

sudo chmod +x /usr/local/bin/audiocaffeine.sh
6. Enter your password when asked for it. You can now run this script from the command line by typing audiocaffeine.sh or double clicking it using "Files".

7. To add it to your Startup Items so it runs at login, begin by opening System Settings > Startup Applications

8. Click the "+" button at the bottom of the window and choose "Custom Command".

9. Add whatever "Name" and "Comment" you want in those fields and add a startup delay if you wish. For the "Command" field enter:

Code: Select all

/usr/local/bin/audiocaffeine.sh
Or you can Browse to that location and select the file using the "Browse" button.

10. Click Save.

11. If you want to run the script now Click on the gears button beside the "-" button.

12. To see if the script is running open System Monitor and "audiocaffeine.sh" should be listed as one of the processes.

13. When audio is playing, the Suspend times in your Power Settings should flip to "Never" after audio is detected. It will take up to a minute as the script only checks once a minute. Your settings should revert the next time it detects no audio.
nashalm

Re: Prevent suspend while audio/music playing. - Solved

Post by nashalm »

hi
this is the first time i take part in your forum
this script works well. i tried the first for cinnamon and it works as intended.

is it possible to create a script for linux mint 19.1 xfce edition

thanx in advance
jaygambrel

Re: Prevent suspend while audio/music playing. - Solved

Post by jaygambrel »

Hi nashalm,

I apologize for my late reply. Thank you very much for the feedback. I have not used XFCE before, but I am open to trying to modify the script. It might take me a while to figure out how to change the power settings in XFCE from the terminal as I don't thing the gsettings command will work in XFCE as it is not Gnome based. I will look into this though.

Thanks again for the feedback.
longor

Re: Prevent suspend while audio/music playing. - Solved

Post by longor »

Hi, I just read this and happened to have xfce desktop environment.
I've added some modification to this script so it can be implemented in xfce
Here is goes:

For ALSA

Code: Select all

#!/bin/sh

# Audiocaffeine - ALSA

# Works in Linux Mint 19 - Cinnamon, Linux Mint 19 - Mate, Ubuntu 18.04 - Gnome

# Script to temporarily set suspend timout for AC and battery to "Never"
# while audio is playing.  It then reverts the settings when audio is no longer detected.

# Determine if a valid desktop environment is running and exit if it doesn't.
    echo "Reported desktop environment: ""$XDG_CURRENT_DESKTOP"
if [ "$XDG_CURRENT_DESKTOP" = "X-Cinnamon" ]; then
    actimeoutid="org.cinnamon.settings-daemon.plugins.power sleep-inactive-ac-timeout"
    batttimeoutid="org.cinnamon.settings-daemon.plugins.power sleep-inactive-battery-timeout"
    disablevalue=0
elif [ "$XDG_CURRENT_DESKTOP" = "MATE" ]; then
    actimeoutid="org.mate.power-manager sleep-computer-ac"
    batttimeoutid="org.mate.power-manager sleep-computer-battery"
    disablevalue=0
elif [ "$XDG_CURRENT_DESKTOP" = "ubuntu:GNOME" ]; then
    actimeoutid="org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type"
    batttimeoutid="org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type"
    disablevalue="nothing"
elif [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
    actimeoutid="xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-ac"
    batttimeoutid="xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-battery"
    disablevalue=14
else
    echo "No valid desktop environment is running"
    exit 1
fi

# Create .config directory to store settings if it doesn't exist.
if [ ! -d ~/.config ]; then
    echo ".config directory not found!"
    echo "Creating ~/.config"
    mkdir ~/.config
fi

# Create audiocaffeine directory to store settings if it doesn't exist.
if [ ! -d ~/.config/audiocaffeine ]; then
    echo "Configuration directory not found!"
    echo "Creating ~/.config/audiocaffeine"
    mkdir ~/.config/audiocaffeine
fi

# Restore previous value for AC suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/acsuspend ]; then
    echo "Restoring previous AC suspend timeout."
    read acsuspendtime < ~/.config/audiocaffeine/acsuspend
    if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
        $actimeoutid -s $acsuspendtime 
    else   
        gsettings set $actimeoutid $acsuspendtime
    fi
    echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
    rm ~/.config/audiocaffeine/acsuspend
fi


# Restore previous value for battery suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/battsuspend ]; then
    echo "Restoring previous battery suspend timeout."
    read battsuspendtime < ~/.config/audiocaffeine/battsuspend
    if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
        $batttimeoutid -s $battsuspendtime
    else
        gsettings set $batttimeoutid $battsuspendtime
    fi    
    echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
    rm ~/.config/audiocaffeine/battsuspend
fi

# Start main loop to check if audio is playing

while true; do

    # Use ALSA to detect if there are any running audio sources.
    if grep -q RUNNING /proc/asound/card*/*p/*/status 2>&1; then

        echo "Audio detected."

        # If AC timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Saving current AC suspend timeout."
            if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
                $actimeoutid > ~/.config/audiocaffeine/acsuspend     
            else
            
                gsettings get $actimeoutid > ~/.config/audiocaffeine/acsuspend
            fi
        fi

        # If battery timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Saving current battery suspend timeout."
            if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
                $batttimeoutid > ~/.config/audiocaffeine/battsuspend
            else
                gsettings get $batttimeoutid > ~/.config/audiocaffeine/battsuspend
            fi        
        fi

        # Set the suspend timouts to Never using gsettings.
        echo "Changing suspend timeouts."
        if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
            $actimeoutid -s $disablevalue
            $batttimeoutid -s $disablevalue
        else        
            gsettings set $actimeoutid $disablevalue
            gsettings set $batttimeoutid $disablevalue
        fi
    else
        echo "No audio detected."
    
        # Restore previous value for AC suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Restoring previous AC suspend timeout."
            read acsuspendtime < ~/.config/audiocaffeine/acsuspend
            if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
                $actimeoutid -s $acsuspendtime
            else            
                gsettings set $actimeoutid $acsuspendtime
            fi
            echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
            rm ~/.config/audiocaffeine/acsuspend
        fi

        # Restore previous value for battery suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Restoring previous battery suspend timeout."
            read battsuspendtime < ~/.config/audiocaffeine/battsuspend
            if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
                $batttimeoutid -s $battsuspendtime
            else                
                gsettings set $batttimeoutid $battsuspendtime
            fi            
            echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
            rm ~/.config/audiocaffeine/battsuspend
        fi

    fi

    # Pause the script for 60 seconds before doing the loop again.
    sleep 60s

done

For pulse

Code: Select all

#!/bin/sh

# Audiocaffeine - ALSA

# Works in Linux Mint 19 - Cinnamon, Linux Mint 19 - Mate, Ubuntu 18.04 - Gnome

# Script to temporarily set suspend timout for AC and battery to "Never"
# while audio is playing.  It then reverts the settings when audio is no longer detected.

# Determine if a valid desktop environment is running and exit if it doesn't.
    echo "Reported desktop environment: ""$XDG_CURRENT_DESKTOP"
if [ "$XDG_CURRENT_DESKTOP" = "X-Cinnamon" ]; then
    actimeoutid="org.cinnamon.settings-daemon.plugins.power sleep-inactive-ac-timeout"
    batttimeoutid="org.cinnamon.settings-daemon.plugins.power sleep-inactive-battery-timeout"
    disablevalue=0
elif [ "$XDG_CURRENT_DESKTOP" = "MATE" ]; then
    actimeoutid="org.mate.power-manager sleep-computer-ac"
    batttimeoutid="org.mate.power-manager sleep-computer-battery"
    disablevalue=0
elif [ "$XDG_CURRENT_DESKTOP" = "ubuntu:GNOME" ]; then
    actimeoutid="org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type"
    batttimeoutid="org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type"
    disablevalue="nothing"
elif [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
    actimeoutid="xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-ac"
    batttimeoutid="xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-battery"
    disablevalue=14
else
    echo "No valid desktop environment is running"
    exit 1
fi

# Create .config directory to store settings if it doesn't exist.
if [ ! -d ~/.config ]; then
    echo ".config directory not found!"
    echo "Creating ~/.config"
    mkdir ~/.config
fi

# Create audiocaffeine directory to store settings if it doesn't exist.
if [ ! -d ~/.config/audiocaffeine ]; then
    echo "Configuration directory not found!"
    echo "Creating ~/.config/audiocaffeine"
    mkdir ~/.config/audiocaffeine
fi

# Restore previous value for AC suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/acsuspend ]; then
    echo "Restoring previous AC suspend timeout."
    read acsuspendtime < ~/.config/audiocaffeine/acsuspend
    if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
        $actimeoutid -s $acsuspendtime 
    else   
        gsettings set $actimeoutid $acsuspendtime
    fi
    echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
    rm ~/.config/audiocaffeine/acsuspend
fi


# Restore previous value for battery suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/battsuspend ]; then
    echo "Restoring previous battery suspend timeout."
    read battsuspendtime < ~/.config/audiocaffeine/battsuspend
    if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
        $batttimeoutid -s $battsuspendtime
    else
        gsettings set $batttimeoutid $battsuspendtime
    fi    
    echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
    rm ~/.config/audiocaffeine/battsuspend
fi

# Start main loop to check if audio is playing

while true; do

    # Use ALSA to detect if there are any running audio sources.
    if pactl list | grep -q "State: RUNNING"; then

        echo "Audio detected."

        # If AC timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Saving current AC suspend timeout."
            if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
                $actimeoutid > ~/.config/audiocaffeine/acsuspend     
            else
            
                gsettings get $actimeoutid > ~/.config/audiocaffeine/acsuspend
            fi
        fi

        # If battery timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Saving current battery suspend timeout."
            if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
                $batttimeoutid > ~/.config/audiocaffeine/battsuspend
            else
                gsettings get $batttimeoutid > ~/.config/audiocaffeine/battsuspend
            fi        
        fi

        # Set the suspend timouts to Never using gsettings.
        echo "Changing suspend timeouts."
        if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
            $actimeoutid -s $disablevalue
            $batttimeoutid -s $disablevalue
        else        
            gsettings set $actimeoutid $disablevalue
            gsettings set $batttimeoutid $disablevalue
        fi
    else
        echo "No audio detected."
    
        # Restore previous value for AC suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Restoring previous AC suspend timeout."
            read acsuspendtime < ~/.config/audiocaffeine/acsuspend
            if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
                $actimeoutid -s $acsuspendtime
            else            
                gsettings set $actimeoutid $acsuspendtime
            fi
            echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
            rm ~/.config/audiocaffeine/acsuspend
        fi

        # Restore previous value for battery suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Restoring previous battery suspend timeout."
            read battsuspendtime < ~/.config/audiocaffeine/battsuspend
            if [ "$XDG_CURRENT_DESKTOP" = "XFCE" ]; then
                $batttimeoutid -s $battsuspendtime
            else                
                gsettings set $batttimeoutid $battsuspendtime
            fi            
            echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
            rm ~/.config/audiocaffeine/battsuspend
        fi

    fi

    # Pause the script for 60 seconds before doing the loop again.
    sleep 60s

done
Of course, a big shout and thanks to jaygrambel for developing this script in the first place!
longor

Re: Prevent suspend while audio/music playing. - Solved

Post by longor »

PS. I do recommend using pulseaudio though. Because jay's Alsa using '/proc/...' to detect whether an audio playing or not, problems emerge when let's just say for example, you pair a bluetooth speaker as the output of your sound.
User avatar
MrEen
Level 23
Level 23
Posts: 18343
Joined: Mon Jun 12, 2017 8:39 pm

Re: Prevent suspend while audio/music playing. - Solved

Post by MrEen »

longor wrote: Sun Dec 13, 2020 12:36 am PS. I do recommend using pulseaudio though. Because jay's Alsa using '/proc/...' to detect whether an audio playing or not, problems emerge when let's just say for example, you pair a bluetooth speaker as the output of your sound.
Good point, and welcome to the forum. :D

Also, another way this can be accomplished on Xfce is with Presentation Mode which can be toggled with this:

Code: Select all

xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/presentation-mode -T
Locked

Return to “Cinnamon”