I was downloading a large file in my browser when my computer went to sleep.
The download was lost.
Any way to prevent that?
I was downloading a large file in my browser when my computer went to sleep.
The download was lost.
Any way to prevent that?
Yeah I had this problem too. The only way to fix it is to not let the computer go to sleep (even if the screen is off, can't remember how to, sorry) , or get a browser that supports suspended downloads. Vivaldi worked for me last time.
When a computer goes to sleep, the computer enters a low power state, only allowing enough power to go the RAM to keep its contents alive. Unfortunately in most cases that also means that the power to the network cards is dropped, causing the system to go to sleep.
Your only option at this point is to stop the system from sleeping. One way is with changing the "blank screen" to never in the power settings.
You could always use a download manager to resume the download at any point, but you'll still have the issue that the download pauses while the system is offline.
Sleeping will interrupt downloads across pretty much all operating systems. You will need to keep the computer awake during downloads.
You can do this manually, but I made myself a little script, which prevents my laptop from sleeping during downloads automatically. I'm not an expert, so I'll instead post all my sources/references where applicable. I hope this is answer will be a useful resource :D
This question inspired my answer:
How can I disable suspend during download, but the screen will still shut down?
I'll explain how to set up my version of the script in the link below:
TODO DONE: I realise that it is better to sense downloads by checking the size of the downloads folder rather than using ifstat
.
This is because if your watching a video from your browser, eg. Youtube, the program will think you are downloading a file when you aren't.
This is the new command:
du -ks ~/Downloads
The program still works, but you cannot trigger sleep manually after a "download"(/watching a YouTube Video) until SLEEP_TIME as elapsed. Then your computer will sleep afterwards if configured to do so in Ubuntu settings.
The python code has been updated and you don't need ifstat
anymore. You can safely uninstall it with:
sudo apt remove ifstat
Create a folder where you will keep all the scripts in this answer. There are 4 scripts in total.
enable-sleep.sh
, and put in the following code:sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
disable-sleep.sh
:sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
disable-sleep-during-downloads.py
Source: How can I disable suspend during download, but the screen will still shut down?
python
and acpi
installed:sudo apt update && sudo apt install python3 && sudo apt install acpi
Create a file called disable-sleep-during-downloads.py
and make it executable.
Paste the following code inside it, and fill in the [[GAPS]]:
#!/usr/bin/env python3
import subprocess
import time
Get the output of a bash command
def get(cmd):
return subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")
Set a minimum speed (KiB/sec) to be considered a download activity.
Would leave as 0 unless you're downloads folder increases in size when your not downloading something from the
internet (extremely unlikely for most users)
NOTE: This means that if you move a file into the downloads folder that is large enough, then sleep will be disabled, but
it will get re-enabled after (LOOP_TIME * NO_OF_ITERATIONS_FOR_CONCLUSION) seconds has elapsed.
SPEED_LIMIT = 0
The amount of time this script does nothing for, before checking if there's another download, in seconds.
We want to make sure this script is not intensive on the CPU, so we make it sleep for several seconds.
(This script has next to no impact on the processor if we sleep for several seconds).
NOTE: If you work in the downloads folder often, then editing and saving files in the Downloads folder can trigger this
script to disable sleep, and make it disable sleep for LOOP_TIME seconds, so just be wary, not really a problem for most users.
This script will still work for you if you don't mind this either. It'll enable sleep after (LOOP_TIME * NO_OF_ITERATIONS_FOR_CONCLUSION) has elapsed
LOOP_TIME = 20
The number of iterations we wait before we conclude no download is happening in a folder.
Checking twice should be good enough, I can't see why it wouldn't be, but this is a configurable constant
so I will leave it here just in case
NO_OF_ITERATIONS_FOR_CONCLUSION = 2
All the folders you want to sense if there's a download going into it
For most users, "~/Downloads" is sufficient, but sometimes applications download into different folders,
You can add any other folders into this list below:
DOWNLOAD_FOLDERS_PATHS = ["~/Downloads", "any/other", "path/'add it'/on"]
DOWNLOAD_FOLDERS_PATHS = ["~/Downloads"]
The command to get the size of the downloads folder
size_of_downloads_cmd = "du -ks "
The command to enable sleep
enable_sleep_cmd = (
r"sudo [[/PATH/TO]]/enable-sleep.sh"
)
The command to disable sleep
disable_sleep_cmd = (
r"sudo [[/PATH/TO]]/disable-sleep.sh"
)
The command to check if the ac-power is connected to the laptop
check_ac_power_is_connected = r"acpi -a | cut -d' ' -f3 | cut -d- -f1"
print(f"### DISABLE SLEEP ONLY DURING DOWNLOADS SCRIPT STARTED ###")
The number of checks before we're certain no downloads are happening
Why we don't keep track of no_of_checks per folder? TL;DR we don't need to
If any single folder has a download, then we don't need to check the rest
so we don't need to keep track of no_of_checks for every folder,
just a single folder which has a download happening (doesn't even need to be the same one)
This initial value will ensure sleep is enabled on first iteration if no downloads
are happening
no_of_checks = NO_OF_ITERATIONS_FOR_CONCLUSION
try:
# Get our initial folder sizes
checks = [
int(get(size_of_downloads_cmd + path + "982heiu2qh&&&*TGI^$%E").split()[0])
for path in DOWNLOAD_FOLDERS_PATHS
]
print("~~ Enabling Sleep ~~")
print(get(enable_sleep_cmd))
is_sleep_enabled = True
print(f"### SETUP COMPLETE, STARTING INFINITE LOOP ###")
while True:
time.sleep(LOOP_TIME)
try:
print("\n###-###-###-###-###")
is_ac_connected = get(check_ac_power_is_connected).strip() == "on"
# To ensure sleep is not enabled mid-download (the case of NO_OF_ITERATIONS_FOR_CONCLUSION = 1)
any_downloads = False
for idx, path in enumerate(DOWNLOAD_FOLDERS_PATHS):
# Get the new folder size
check_next = int(get(size_of_downloads_cmd + path).split()[0])
# I used integer divide because we're I doubt we need to sense downloads of speeds smaller than 1KiB/s
# In case you ever need to, replace // with / below
current_download_speed_in_kib_s = (
check_next - checks[idx]
) // LOOP_TIME
# For testing purposes. Doesn't matter if you leave them uncommented or not
print(f"\nChecking path: {path}")
print(f"LOOP_TIME = {LOOP_TIME}")
print(f"is_ac_connected = {is_ac_connected}")
print(f"check_1 = {checks[idx]}")
# Now we have our download speed, we need to remember the previous download folder's size, to sense downloads
# This should always update, regardless of if there is a download or not
# because a download is a rate of increase in a folder, not is the folder bigger
# eg. I removed a large file from downloads during a download:
# - The folder is not bigger anymore, and may never be bigger than before
# the large file was removed. NO_OF_ITERATIONS_FOR_CONCLUSION prevents enabling sleep in such unexpected cases
# - But soon it will sense a rate of increase again from the download, and disable
# sleep again
checks[idx] = check_next
print(f"check_2 = {check_next}")
print(f"SPEED_LIMIT = {SPEED_LIMIT}KiB/s")
print(
f"current_download_speed_in_kb_s = {current_download_speed_in_kib_s}KiB/s"
)
# If the ac-power is connected, and the current download speed is above the speed limit
if is_ac_connected and current_download_speed_in_kib_s > SPEED_LIMIT:
if is_sleep_enabled:
print("!! Disabling Sleep !!")
print(get(disable_sleep_cmd))
is_sleep_enabled = False
# Get ready to start checking, or reset if a download is still happening
# (A check will be completed before making a conclusion, That's why its 1 not 0)
no_of_checks = 1
any_downloads = True
# There's no need to check other folders once we found a download is happening
break
# We increment after the for loop, since each folder has only been checked once during the for loop, or enable sleep again
# Sleep won't enable unless no_of_checks is not reset and there's no downloads and we've checked enough times
print(
"\n"
+ (
"All checks complete."
if no_of_checks >= NO_OF_ITERATIONS_FOR_CONCLUSION
else f"Check {no_of_checks} complete."
)
)
if (
(not is_sleep_enabled)
and (not any_downloads)
and no_of_checks >= NO_OF_ITERATIONS_FOR_CONCLUSION
):
print("~~ Enabling Sleep ~~")
print(get(enable_sleep_cmd))
is_sleep_enabled = True
elif no_of_checks < NO_OF_ITERATIONS_FOR_CONCLUSION:
no_of_checks += 1
except subprocess.CalledProcessError:
print("?? There was a CalledProcessError ??")
print(f"Sleep is {('en' if is_sleep_enabled else 'dis')}abled")
except Exception as e:
print(
f"There was an error. This script attempts to enable sleep again in the case of failure.\n{e}"
)
print("~~ Enabling Sleep ~~")
print(get(enable_sleep_cmd))
disable-sleep-during-downloads.sh
and paste the following command (this file won't need sudo privileges):flock --verbose -n /var/lock/disable-sleep-during-downloads.lock python3 [[PATH/TO]]/disable-sleep-during-downloads.py
flock
ensures there's only one instance of this script running at any one time.
Make this file executable. (Source: https://www.tutorialspoint.com/ensure-only-one-instance-of-a-bash-script-is-running-on-linux)
SLEEP_TIME = 60
for testing purposes)[[PATH/TO]]/disable-sleep-during-downloads.py
And try running a download, or you can use the internet speed test and see if it disables sleep when the ac-power is plugged into your laptop, during the download test, and enables sleep after the test. (Use Ctrl+C to stop the script)
SLEEP_TIME
to the time it takes your laptop to sleep on ac power.enable-sleep.sh
script run as soon as the ac-power is disconnected:Then reboot your system.
Try running the script again:
[[PATH/TO]]/disable-sleep-during-downloads.py
If it failed saying it couldn't get the lock, then that means the script is successfully running, because this means there's already an instance of it running.
Now you can go into the regular systems settings, and set your desired sleep settings like normal. This script does not interfere with those settings, and your computer will sleep like normal, but not during a download.
I hope this helps you :D
Source: https://unix.stackexchange.com/questions/174028/killing-a-shell-script-running-in-background
In the case you need to turn off this script after it has autostarted, run this command, and enable sleep again:
killall disable-sleep-during-downloads.sh
sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
Then go into the settings and stop this script from autostarting.
You can prevent that the computer goes sleeping with caffeine
:
Gnome-shell:
Install from https://extensions.gnome.org/extension/517/caffeine/
Unity or Mate:
sudo apt install caffeine
Then run Caffeine Indicator
from the Menu and then click the indicator icon -> "Activate".
Yes, configure your computer to not go to sleep (somewhere in the power options).
Applications are able to prevent sleep, so you could find a download client that does it. For FTP, Filezilla has this capability, but I don't know a HTTP download client that does. But since you are on Ubuntu, maybe Keep.Awake can help.