Check for download before running command

This works so I do not have to manually hit my sleep button.

Is there a way to make it not run if a download is occuring? (Using my browser)

#!/bin/bash
#
# Suspend computer if no mouse/keyboard activity for 10 minutes
#
# The time is in milliseconds !! 3000 = 3 seconds
# 1 minute = 1000 x 60 = 60000 10 minutes = 600000
while :; do
  if (( $(xprintidle) >= 600000 )); then
    systemctl suspend
    exit 
  fi
  sleep 0.5
done

--- Post updated at 03:50 PM ---

I found a problem.

When computer returns from suspend, my script is no longer running.

Given that your script exit s after running systemctl suspend , why would you expect it to continue running?

Furthermore, if your script doesn't exit, will systemctl suspend succeed or will it block waiting for your script to terminate?

Script does not succeed without an exit.

It does work and suspends when time has elapsed.

So, I repeat: "Given that your script exit s after running systemctl suspend , why would you expect it to continue running?"

Removing exit solved the problem.

As regarding check if a download is occuring,

An active download will has the .part extension.

ubuntu-mate-18.10-desktop-amd64.iso.part

I will write some code to check for .part files in my download directory.

for file in /home/andy/Downloads/*.part ; do
    if [[ -f $file ]]; then
        echo "File download in progress."
        echo "Now exiting."
        sleep 2 
        break
    fi
done

I have overlooked something here.

If a .part file is present, it still suspends?

It's probably something simple.

#!/bin/bash
#----------------------------------------------------------------------------
# SUSPEND COMPUTER IF NO MOUSE/KEYBOARD ACTIVITY FOR 5 MINUTES
#
# The time is in milliseconds !! 300000 = 5 minutes
#
# This is good to put in startup programs
#----------------------------------------------------------------------------

for file in /home/andy/Downloads/*.part ; do
    if [[ -f $file ]]; then
        echo "File download in progress."
        echo "Computer can not suspend until download is complete."
        echo "Now exiting."
        sleep 2 
        break
    fi
done

echo "This script will suspend computer in 5 minutes if there is no mouse or keyboard activity."
while :; do
  if (( $(xprintidle) >= 300000 )); then
    systemctl suspend
fi
  sleep 0.5
done

The code in your script:

        echo "Now exiting."
        sleep 2 
        break

implies that you believe that writing the string Now exiting. to standard output from your script causes the script to stop running. It does not do that. It simply writes that text and continues running the rest of the code in your script. In this case, it sleeps for two seconds and breaks out of the loop looking for files being downloaded. And, then it immediately starts testing for the conditions you have set up to be verified as being present before you suspend your computer. If you want your script to exit (as indicated by your echo statement), you need to actually include an exit statement in your code!

I think I am hopeless with the if, then while, etc.

I had it working until I added code to check for downloads.

I can definitely live without it.

This script will suspend computer in 10 minutes if there is no mouse or keyboard activity.
/home/andy/bin/test_dl.sh: line 26: syntax error near unexpected token `fi'
/home/andy/bin/test_dl.sh: line 26: `fi'

for file in /home/andy/Downloads/*.part ; do
    if [[ -f $file ]]; then
        echo "File download in progress."
        echo "Computer can not suspend until download is complete."
        echo "Now exiting."
        exit
    fi
done

echo "This script will suspend computer in 10 minutes if there is no mouse or keyboard activity."
sleep 1
  if (( $(xprintidle) >= 600000 )); 
    systemctl suspend
then

fi

Please note that I have never seen any shell complain about a problem on line 26 in a shell script that only contains 16 lines???

The syntax for an if command is:

if condition
then	command...
fi

not:

if condition
	command...
then
fi
1 Like

Help me out - threads over threads you are trying to mimic a function / behaviour that "professional" packages like screen savers offer at no cost. Why?

1 Like

Hi drew...

Tell us what your project is or what you are trying to achieve then perhaps we can help you with it as a complete proper entity.

Some of the snippet code threads you have started are not portable at all, 'xinput' for example will not work on at least APPLE gear and CygWin for Windows as they don't have it.
'systemctl' will not work on many *NIX like flavours as they don't have it either. I can't test any of your code on this MacBook Pro without rebooting into Linux Mint 19.
This can become tedious...

EDIT:
Just checked and Linux Mint 19 does NOT have 'xprintidle' either on a default install.

Thanks for everyone's help.

I am happy with this. Checking for a download is not critical.

#!/bin/bash
#----------------------------------------------------------------------------
# SUSPEND COMPUTER IF NO MOUSE/KEYBOARD ACTIVITY FOR 10 MINUTES
#
# The time is in milliseconds !! 300000 = 5 minutes
#
# This is good to put in startup programs
#
# Much help from The UNIX and Linux Forums  - Free Tech Support
#----------------------------------------------------------------------------
echo "This script will suspend computer in 10 minutes if there is no mouse or keyboard activity."
while :; do
  if (( $(xprintidle) >= 600000 )); then
    systemctl suspend
fi
  sleep 0.5
done

Screen savers do not save as much energy nor reduce computer wear and tear like suspend does.

I'm afraid you're totally off track here. Screen savers - esp. on Ubuntu 18.10 - cooperate with power management and can be configured to include suspend (or even hibernation, I think).

Hi drew77...

As far as I am aware systemctl suspend is simlar to hibernate.
Wouldn't this interrupt your downloads or am I missing something?
EDIT:
Apparently I am wrong, the machine is not turned off like hibernate but is in a HW super low power mode.

I have ubuntu mate 18.04.