0

I need help from specialist. Between 00 am and 6 am my WiFi is unlimited, this is when i start my downloads. I usually download movies from torrents and ISO files that I test between this time. My question is how do I stop my WiFi at 6am so it turns off everything ? Because there are files that takes up to 20 GB and it doesn’t finish in one night and I have to stop it at 6. What should I do in that case ? Should I write a python program or should I change something in the settings ?

I really need help for that, if someone knows a program that I can download that makes all the work for me please tell me. Btw I can’t enter in my tplink router from the web page, so I should try and find an application thank you.

  • Do you know how to run a .py (python) program? Do you have Python installed? – Martin Medro Nov 23 '20 at 12:13
  • @AakashSinghBais yes –  Nov 23 '20 at 12:14
  • Basically you need a Python program that can turn-on Wifi on your PC at 00:00 and then turn it back off at 6am...? – Martin Medro Nov 23 '20 at 12:16
  • @AakashSinghBais yes –  Nov 23 '20 at 12:17
  • @AakashSinghBais thanks for the codes but how should i turn on the wifi again after that ? –  Nov 23 '20 at 12:28
  • Program turns it on, and then back off again ; )... No worries, the code will have to be added to Startup Apps list to be run at system startup...and it will keep running in the background I guess. You can turn the Wifi back on the usual way. – Martin Medro Nov 23 '20 at 12:32
  • @AakashSinghBais i tried the code and did everything like you and for me it’s wlp2s0 but it doesn’t turn off the wifi –  Nov 23 '20 at 12:42
  • @AakashSinghBais i just downloaded ifupdown i needed it sorry for the mistake but now it’s saying that it’s the wrong interface wlp2s0 –  Nov 23 '20 at 12:45

2 Answers2

2

Here is a simple Python program to turn the wifi on at 00:00 and then back off at 06:00 :

import os
import time
from datetime import datetime
import sys

while(1): now = datetime.now() if now.hour == 0: print("Starting Wifi...\n") os.system("nmcli radio wifi on") if now.hour == 6: print("Turning Wifi off...\n") os.system("nmcli radio wifi off") sys.exit() time.sleep(60)

Just save this as a .py file, and then add it to the startup services on your Linux system so that it starts in the background when the computer is started and keeps running. The script will automatically terminate after it Turns the wifi off at 06:00 ± 1 min.

NOTE: The script checks the current hour every 1 min, so the actual time when the Wifi connection is tripped or restarted may get delayed by 1min, if you want it to check it faster, you can change the time.sleep(60) to let's say time.sleep(30) to make it check time every 30 seconds.


UPDATE: (To turn off at 05:50), use the following, you can tweak the hour and the seconds to your liking in the line if now.hour == 5 and now.minute >= 50: of the code below:

import os
import time
from datetime import datetime
import sys

while(1): now = datetime.now() if now.hour == 0: print("Starting Wifi...\n") os.system("nmcli radio wifi on") if now.hour == 5 and now.minute >= 50: print("Turning Wifi off...\n") os.system("nmcli radio wifi off") sys.exit() time.sleep(60)

Adding the Script to STartup Apps

  1. Check your Python version by the which command in terminal, for Python2 use: which python2, for python3 use which python3. Copy the location of the python. Terminal screenshot
  2. In your Ubuntu's Applications list, look for Startup Applications and run it.
  3. Add a new App by selecting "Add"
  4. Give your startup service a name, and type the command to execute. For me, since my Python version is 3, located at /usr/local/bin/python3 with python program at /Desktop/wifi.py, I use the following in the command : /usr/local/bin/python3 /Desktop/wifi.py
  5. Next, simply add the service. enter image description here

UPDATE: a program that displays messages only once. Added Feature: if wifi is turned off accidentally during the time when the program expects it to be on, the program will automatically restart the wifi. So the program will also be checking the internet connection and prevent wifi from being turned off between the turn on and the turn off times.

import os
import time
from datetime import datetime
import sys
import urllib.request

def connect(host='http://google.com'): try: urllib.request.urlopen(host) #Python 3.x return 1 except: return 0 preWifi = 0 while(1): now = datetime.now() wifi = connect() if wifi == 1 and wifi != preWifi: print('Wifi On.') if now.hour == 0 and wifi != 1: print("Starting Wifi...\t | Time", now, "\n") os.system("nmcli radio wifi on") print("Reconnecting...please stand by.\t | Time =", now, "\n") while(not connect()): time.sleep(0.5) if now.hour == 5 and now.minute == 50 and wifi == 1: print("Turning Wifi off...\t | Time", now, "\n") os.system("nmcli radio wifi off") sys.exit() preWifi = wifi time.sleep(1)

Hope this works perfectly! :D

To completely turn off messages, just save the program with a .pyw extension instead of .py.

  • 1
    IT PERFECTLY WORKEEED THANK YOU ❤️❤️❤️❤️❤️❤️ –  Nov 23 '20 at 14:01
  • Aakash do you have other python programs or cool stuff like that to share with me ? That would be really cool to see what you have made... –  Nov 23 '20 at 20:45
  • Nothing much...just learning. Helping others is fun! – Martin Medro Nov 23 '20 at 20:58
  • well then thank you very much i will now start my downloads in 1 hour –  Nov 23 '20 at 20:59
  • For the time.sleep if i want him to check every 30 minutes how do i chamge it ? –  Nov 23 '20 at 21:08
  • @Elie time.sleep() takes value in seconds, so just make it time.sleep(30) in the last line of the code. – Martin Medro Nov 24 '20 at 06:07
  • https://askubuntu.com/q/1295243/1072569 i have smtg new if you would like to take a look @AakashSinghBais –  Nov 25 '20 at 17:54
  • please i sent you the link up can you check it. it's 2 programs written in c but i don't know how to set them up. Can you please take a look for me and then tell me what to do. –  Nov 25 '20 at 22:32
0

The simplest way would be to use a cron job.

First, identify the name of your wireless device with ls /sys/class/net. You’ll see something like:

eth0   lo   wlp3s0`

Then type crontab -e to open crontab, and add this line:

59 05 * * * ifdown wlp3s0

That will shut the WiFi off at 5:59am.