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
- 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.

- In your Ubuntu's Applications list, look for Startup Applications and run it.
- Add a new App by selecting "Add"
- 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
- Next, simply add the service.

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
.