Check for download before running command


 
Thread Tools Search this Thread
Operating Systems Linux Ubuntu Check for download before running command
# 1  
Old 03-23-2019
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)

Code:
#!/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.

Last edited by Don Cragun; 03-23-2019 at 09:48 PM.. Reason: Add CODE tags again.
# 2  
Old 03-23-2019
Given that your script exits 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?
# 3  
Old 03-23-2019
Quote:
Originally Posted by Don Cragun
Given that your script exits 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.
# 4  
Old 03-23-2019
Quote:
Originally Posted by drew77
Script does not succeed without an exit.

It does work and suspends when time has elapsed.
So, I repeat: "Given that your script exits after running systemctl suspend, why would you expect it to continue running?"
# 5  
Old 03-23-2019
Removing exit solved the problem.

As regarding check if a download is occuring,

An active download will has the .part extension.

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

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

Code:
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


Last edited by drew77; 03-23-2019 at 10:58 PM..
# 6  
Old 03-24-2019
I have overlooked something here.

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

It's probably something simple.



Code:
#!/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

# 7  
Old 03-24-2019
The code in your script:
Code:
        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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check if process is running if not then use command

Hello, Could someone do the following bash ubuntu script for me? I have 5 screen processes of bot: SCREEN -dmS Xbot_instance_1 php core.php -i 1 SCREEN -dmS Xbot_instance_2 php core.php -i 2 SCREEN -dmS Xbot_instance_3 php core.php -i 3 SCREEN -dmS Xbot_instance_4 php core.php -i 4 ... (2 Replies)
Discussion started by: kotch
2 Replies

2. Shell Programming and Scripting

Check if remote destination is available before running scp command

I have a script on a Linux box which scp the files to windows server without any issues. but there are time frames where the windows server will not be available due to maintenance. hence I need to check if the remote location is available before running the scp command. scp... (3 Replies)
Discussion started by: gpk_newbie
3 Replies

3. Shell Programming and Scripting

Command to check only Autosys running jobs with autorep like command

Hi, Is there any specific command to use to check only say Running jobs via autorep or similar command for Autosys? (0 Replies)
Discussion started by: sidnow
0 Replies

4. Shell Programming and Scripting

Check to see if script is already running

Happy New Year Is there a quick way to check to see if a script is already running. I want to put in a check in the script to exit, if already running. Currerntly i can only think of doing it the following way. # ps -ef | grep -i 3_HOUSEKEEPING_FFTVTL_TO_FFTDSSU_DUPLICATION.ksh |... (5 Replies)
Discussion started by: Junes
5 Replies

5. Shell Programming and Scripting

Check email and download attachment

Hi, I had search the web for a script to download email, but failed to found one. I need a bash or perl script that will check for email originating from an address such as john@rambo.com and download the .zip attachment into a specified folder. Anyone could assist or give me some... (1 Reply)
Discussion started by: mynullvoid
1 Replies

6. Shell Programming and Scripting

Check what cron is running

How to check what cron is scheduled to run? I don't want to modify anything. Thanks (1 Reply)
Discussion started by: stevensw
1 Replies

7. UNIX for Dummies Questions & Answers

Command to check if a particular process is running

Hi What is the best command to check if a particular process is running in a linux server or not To check any java process, I use the below command. ps -ef |grep jvm When I execute the above command, it lists me all the processess . The above command should ideally return only the... (6 Replies)
Discussion started by: vr3w3c9
6 Replies

8. UNIX and Linux Applications

How to check if process is running?

Hi I would like to check if an instance of a script is already running. I've seen many different examples, but I haven't the slightest idea as to how to do this. Can you please help. Thank you. (5 Replies)
Discussion started by: ladyAnne
5 Replies

9. Shell Programming and Scripting

Check if Process is running

Hi , I have a csh code below which check the process if it's running. Can any expert advise me on the following: 1) what does this notationmean ">!" and how is it different from the append ">" notation ? 2) how does "setenv" work in this code ? # Check whether there is a running... (3 Replies)
Discussion started by: Raynon
3 Replies

10. UNIX for Dummies Questions & Answers

Check if deamons are running

Does anyone know if there is a UNIX-tool available that constantly will check if (some specific) deamons are running and will notify (via email) if one has failed/stopped? I searched the web, but so far didn't find anything. (3 Replies)
Discussion started by: W2W
3 Replies
Login or Register to Ask a Question