113

Can I run a google colab (free edition) script and then shut down my computer?

I am training several deeplearning models with crossvalidation, and therefore I would like to know if I can close the window or the computer with the training running at the same time in the cloud.

rmtheis
  • 5,992
  • 12
  • 61
  • 78
S.MC.
  • 1,491
  • 2
  • 9
  • 17
  • Maybe this is something you are looking for https://unix.stackexchange.com/questions/4442/how-to-shut-down-the-computer-after-a-task-has-been-completed Do you want to send some message to your PC from google-colab machine? – Konrad Mar 07 '19 at 19:05
  • 1
    Thank you for answering, but this is not what i am looking for. – S.MC. Mar 07 '19 at 20:42

5 Answers5

118

Edited: With the browser closed, a Colabs instance will run for at most 12 hours 90 minutes before your runtime is considered idle and is recycled.

At the same time, it would be good practice to save your model weights periodically to avoid losing work.


Details:

There are no official references for 'Idle' and 'Maximum Lifetime' durations, but testing done by Korakot Chaovavanich shows that:

  • The 'maximum lifetime' of a running notebook is 12 hours (browser open)
  • An 'Idle' notebook instance cuts-off after 90 minutes
  • You can have a maximum of 2 notebooks running concurrently
  • If you close the notebook window and open it while the instance is still running, the cell outputs and variables will still persist. However if the notebook instance has been recycled, your cell outputs and variables will no longer be available.

A sneaky workaround you can try is to have the Colabs instance open in your mobile browser in order to prevent the instance from being considered "Idle".


Your own milage will vary as from personal experience I sometimes get slighty shorter durations. But as long as you checkpoint your models (periodically save the training weights), you should be able to get a substantial amount of training done before the VM is recycled, after which you could simply load the weights into the model on a new VM instance and resume training.

If you'd like to train your model for more than 12 hours at a single go however, you can run Google Colaboratory on a local instance or a standard Jupyter Notebook. But you would forego the free GPU/TPU that Colaboratory provides. (Checkpointing would still be a good idea here!)


Relevant questions from the Google Colaboratory FAQ:

Where is my code executed? What happens to my execution state if I close the browser window?

Code is executed in a virtual machine dedicated to your account. Virtual machines are recycled when idle for a while, and have a maximum lifetime enforced by the system.

How may I use GPUs and why are they sometimes unavailable?

Colaboratory is intended for interactive use. Long-running background computations, particularly on GPUs, may be stopped. Please do not use Colaboratory for cryptocurrency mining. Doing so is unsupported and may result in service unavailability. We encourage users who wish to run continuous or long-running computations through Colaboratory’s UI to use a local runtime.

Community
  • 1
  • 1
evantkchong
  • 2,251
  • 3
  • 14
  • 29
  • 1
    Based on my own recent experience, I believe Colab will allocate you at most 12 hours of GPU usage, after which there is roughly an 8 hour cool-down period before you can use compute resources again. In my case, I could not connect to an instance even without a GPU. I'm not entirely sure about this next bit but I think if you run say 3 instances at once, your 12 hours are depleted 3 times as fast. I don't know after what period of time the 12 hour limit resets, but I'd guess maybe a day. – DerekG Feb 04 '20 at 17:30
  • 2
    have you tested keeping it running on a smartphone? – PayamB. Apr 06 '20 at 11:50
  • 1
    The last time I tried this was in early 2019, I'm not sure about whether it still works today. – evantkchong Apr 07 '20 at 01:43
  • What is considered 'idle' state? If I was to not click on the colab tab, but have it open in my browser, would this also be considered 'idle' state? And what about the 12 hours max restriction - is it only holding if you use a hosted GPU/TPU or the restriction also in place when you run colab locally? – NeStack Feb 03 '21 at 17:07
  • What's the point of being always online? it is just stupid. Training a model could take weeks to get completed. – pouya Jun 14 '22 at 05:35
27

Open your browser console and copy and paste the code bellow, that will avoid you to get kicked off for being idle.

function ClickConnect() {
console.log("Working"); 
document
  .querySelector('#top-toolbar > colab-connect-button')
  .shadowRoot.querySelector('#connect')
  .click() 
}
setInterval(ClickConnect, 60000)

Ps. You will get kicked after 12 hours no matter what, so be sure that you are saving your progress to your Google Drive.

Juliano Petronetto
  • 1,067
  • 13
  • 15
  • 2
    I did that. and now I am annoyed by the dialog always poping up. How can I undo this? or disable this? – gota Jan 12 '21 at 13:40
  • 7
    If you got annoyed it means you're looking to it, if you're looking to it what's the point to use it if you can just move your mouse to avoid getting kicked for being idle? – Juliano Petronetto Jan 14 '21 at 10:26
  • 3
    yes. I was refering to using it while being away from my screen for an extended period of time but then returning back to continue working on it. – gota Jan 15 '21 at 12:32
  • 2
    Sort-of-related tip: create a VM and then VNC into it for long-running notebooks. That way, you can shut down your current machine or close all windows and not have to worry about losing your work. I've tried all other tricks, and this works the best for me. – philipkd Feb 15 '21 at 16:04
  • Like @gota I also used this and had no idea how to stop it. I turned the interval down to 3 to check it worked and then the instance crashed. How do you turn it off? – codeananda Mar 12 '21 at 21:13
  • @AdamMurphy You can probably try to clear out the interval timer. – cyperpunk Sep 27 '21 at 19:57
  • @cyperpunk can you be a bit more precise in how I would do that? Do you mean deleting the number in the `setInterval` call? – codeananda Sep 29 '21 at 07:25
  • 2
    @AdamMurphy Please refer https://www.geeksforgeeks.org/javascript-cleartimeout-clearinterval-method/ – cyperpunk Oct 04 '21 at 08:16
  • Can you give some more instructions where in Google Chrome to paste the code snippet? Just paste it in the white space under "settings"->"more tools"->"developer tools"-> "console"? Or should I go under sources and create a new file for that? a screen shot would be great! thx – NeStack May 26 '23 at 12:40
4

Shell Loop

for me, I choose ,chroot into VM by ssh Ngrok and run command shell (loop) in background
while true;do sleep 300; done & I'm using&` for to run this command in the background so I can exit from website of colab and leave the VM working, for 12h

ssh Ngrok

The best way to use ssh in colab , you must sign one time in ngrok.com and get your OAuth key and add this cell to your colab notebook

#@title **SSH**

! pip install colab_ssh --upgrade &> /dev/null

Ngrok = True  

try:
    if username:
        pass
    elif password:
        pass
except NameError:
    !echo "root:root"   | chpasswd
    username='root'
    password='root'




#@markdown [OAuth](https://dashboard.ngrok.com/auth)
ngrokToken = "" #@param {type:'string'}


def runNGROK():
    from colab_ssh import launch_ssh
    from IPython.display import clear_output
    launch_ssh(ngrokToken, password)
    clear_output()

    print("ssh", username, end='@')
    ! curl -s http://localhost:4040/api/tunnels | python3 -c \
            "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'][6:].replace(':', ' -p '))"










if Ngrok:
    if ngrokToken == "":
        print("No ngrokToken Found, Please enter it")
    else:
        runNGROK()

now past your OAuth key in Edite text in colab cell , after 3-9 seconde you see your ssh like that

ssh root@2.tcp.ngrok.io -p 13225

dont forget change password of root user (default is root) ,for more security, you should be change linux password of root user !echo "root:new password" | chpasswd and from python variable by : password =password='new password' , you must matching linux password and var python password , look in this ligne above:

print("No user found using username and password as 'root'")
    !echo "root:t"   | chpasswd
    username='root'
    password='root'

Dhia Djobbi
  • 1,176
  • 2
  • 15
  • 35
nextloop
  • 166
  • 9
2

use multiprocess in python make one other function and start while loop there! that while loop won't let it sleep!

shailu
  • 190
  • 1
  • 10
-2

Yes, you can run a script on Google Colab (free edition) and then close the window or shut down your computer without interrupting the training process. Google Colab provides cloud-based computing resources, and the code execution happens on Google's servers, not on your local machine.

Here's how it works:

  • You write and run your code in a Google Colab notebook.
  • The code is executed on Google's remote servers.
  • The notebook interface you see in your browser is just an interface to interact with the code execution.
  • Once you've started the training process in a Colab notebook and it's running, you can safely close the browser window, shut down your computer, or even disconnect from the internet. The code will continue to run on Google's servers until it completes or until you manually stop it.

However, there are a few things to keep in mind:

Google Colab sessions are not permanent. If you leave your session idle for a certain period of time (typically around 12 hours), it might be disconnected. If the session is disconnected, the code will stop running, and you'll lose any unsaved data in that session.

If you close a Colab notebook tab in your browser and later want to resume working on it, you can find the notebook in your Google Drive under the "Colab Notebooks" folder.

Make sure your code doesn't rely on any open connections or browser-based interactions since you won't be interacting with the notebook while your computer is off.

Depending on the complexity and time needed for your deep learning training, it's a good practice to monitor your training progress occasionally to make sure everything is running smoothly.

  • AI-generated answers are not accepted on this site. See https://meta.stackoverflow.com/questions/421831/temporary-policy-chatgpt-is-banned – Eric Aya Aug 21 '23 at 05:58