Prevent suspend while audio/music playing. - Solved

Please post suggestions for improvement of Cinnamon here
https://github.com/linuxmint/Cinnamon
Post Reply
jaygambrel
Level 1
Level 1
Posts: 6
Joined: Thu Jul 13, 2017 10:59 am

Prevent suspend while audio/music playing. - Solved

Post by jaygambrel » Fri Aug 03, 2018 1:01 pm

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 jaygambrel on Sun Aug 12, 2018 9:06 pm, edited 2 times in total.

jaygambrel
Level 1
Level 1
Posts: 6
Joined: Thu Jul 13, 2017 10:59 am

Re: Prevent suspend while audio playing.

Post by jaygambrel » Tue Aug 07, 2018 10:32 pm

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
Level 1
Level 1
Posts: 6
Joined: Thu Jul 13, 2017 10:59 am

Re: Prevent suspend while audio playing. - Solved

Post by jaygambrel » Sun Aug 12, 2018 5:28 pm

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.

Post Reply

Return to “Cinnamon”