Alexander Soare
Alexander Soare

Reputation: 3247

How to terminate current colab session from notebook cell

I'm trying to be a good citizen and make sure my notebook session is terminated immediately after running even if I'm not sitting at my machine.

Is there any code I can run in a notebook cell to achieve this?

Upvotes: 18

Views: 3832

Answers (4)

Aplefull
Aplefull

Reputation: 136

We have a way to do this correctly now:

from google.colab import runtime
runtime.unassign()

Upvotes: 7

Ananthu
Ananthu

Reputation: 159

This might work give it a try.

!kill -9 -1

Upvotes: -1

Ayan Chawla
Ayan Chawla

Reputation: 152

!pkill --oldest

!pkill is used to kill the program and --oldest is used to kill the oldest program

Upvotes: -1

Nicolò Gasparini
Nicolò Gasparini

Reputation: 2396

A rather ugly but efficient solution might be

!kill $(ps aux | awk '{print $2}')

ps aux will give you a list of any process running in the machine, awk '{print $2}' will extract the PID of every process currently running and finally kill will stop them, sending a SIGTERM signal.

This will give you a message "Runtime disconnected" and the session will be closed, you can see it worked under "Runtime" > "Manage sessions".

You can see the available system aliases here

Upvotes: 6

Related Questions