I was looking for a solution until I found a Python3 that randomly moves the mouse back and forth and clicks, always on the same place, but that's enough to fool Colab into thinking I'm active on the notebook and not disconnect.
import numpy as np
import time
import mouse
import threading
def move_mouse():
while True:
random_row = np.random.random_sample()*100
random_col = np.random.random_sample()*10
random_time = np.random.random_sample()*np.random.random_sample() * 100
mouse.wheel(1000)
mouse.wheel(-1000)
mouse.move(random_row, random_col, absolute=False, duration=0.2)
mouse.move(-random_row, -random_col, absolute=False, duration = 0.2)
mouse.LEFT
time.sleep(random_time)
x = threading.Thread(target=move_mouse)
x.start()
You need to install the needed packages: sudo -H pip3 install <package_name>
You just need to run it (in your local machine) with sudo
(as it takes control of the mouse) and it should work, allowing you to take full advantage of Colab's 12h sessions.
Credits: For those using Colab (Pro): Preventing Session from disconnecting due to inactivity