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.
#!/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
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
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?
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.
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
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).
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.