├── LICENSE.md
├── README.md
├── automate-colab.service
├── automate-colab.timer
├── automate.sh
├── automateGUI.sh
├── move.sh
├── sample.ipynb
└── script.py
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 asgaralipq
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # automate-colab-script-service
2 |
3 |
4 | A service and a script to run a Google Colab file in headless mode in the background at scheduled time and download the generated artifact to respective directory.
5 |
6 | ## Steps the script performs:
7 |
8 | 1. Open the specified colab file.
9 | 2. Run the project.
10 | 3. Wait until the required artifact is downloaded.
11 | 4. After download, close the web driver.
12 | 5. Tag the downloaded artifact with Date and Time.
13 | 6. Move the artifact to required directory wherever your datastore is.
14 |
15 | ## Sample run on a Colab Project which creates a .txt file:
16 |
17 |

18 |
19 | ## Sample run on a Machine Learning Model that generates a .pkl file:
20 |
21 | 
22 |
23 | ## Creating and starting a systemd service for the .sh script so it gets executed automatically:
24 |
25 | 
26 |
27 | 
28 |
--------------------------------------------------------------------------------
/automate-colab.service:
--------------------------------------------------------------------------------
1 | [Unit]
2 | Description=Service to automate my google colab project.
3 | After=network.target
4 |
5 | [Install]
6 | WantedBy=multi-user.target
7 |
8 | [Service]
9 | Type=simple
10 | ExecStart=/usr/sbin/automate.sh
11 | WorkingDirectory=/home/asgaralipq/Work/Colab-Script/
12 | StandardOutput=journal
13 | StandardError=journal
--------------------------------------------------------------------------------
/automate-colab.timer:
--------------------------------------------------------------------------------
1 | [Unit]
2 | Description=Timer for automate-colab.service
3 | Requires=automate-colab.service
4 |
5 | [Timer]
6 | Unit=automate-colab.service
7 | OnBootSec=15min
8 | OnCalendar=*-*-* 04:07:30
9 | Persistent=true
10 |
11 | [Install]
12 | WantedBy=timers.target
--------------------------------------------------------------------------------
/automate.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | MOZ_HEADLESS=1 /usr/bin/python /home/asgaralipq/Work/Colab-Script/script.py
4 | bash /home/asgaralipq/Work/Colab-Script/move.sh
5 | killall -q -9 firefox
--------------------------------------------------------------------------------
/automateGUI.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | /usr/bin/python /home/asgaralipq/Work/Colab-Script/script.py
4 | bash move.sh
--------------------------------------------------------------------------------
/move.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | timestamp=$(date -Is|tr : -)
3 | mv Downloads/copy.txt Downloads/copy$timestamp.txt
4 | mv Downloads/copy$timestamp.txt Datastore/
--------------------------------------------------------------------------------
/sample.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "sample.ipynb",
7 | "provenance": [],
8 | "collapsed_sections": []
9 | },
10 | "kernelspec": {
11 | "display_name": "Python 3",
12 | "name": "python3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "code",
21 | "metadata": {
22 | "colab": {
23 | "background_save": true,
24 | "base_uri": "https://localhost:8080/",
25 | "height": 17
26 | },
27 | "id": "nqRos8zhO2eT",
28 | "outputId": "f2636e2f-82a3-4a6f-b17b-7376bbbaa07d"
29 | },
30 | "source": [
31 | "from google.colab import files\n",
32 | "import time\n",
33 | "with open(\"copy.txt\", \"w\") as file:\n",
34 | " file.write(\"Hello\")\n",
35 | "\n",
36 | "files.download(\"copy.txt\")"
37 | ],
38 | "execution_count": null,
39 | "outputs": [
40 | {
41 | "output_type": "display_data",
42 | "data": {
43 | "application/javascript": [
44 | "\n",
45 | " async function download(id, filename, size) {\n",
46 | " if (!google.colab.kernel.accessAllowed) {\n",
47 | " return;\n",
48 | " }\n",
49 | " const div = document.createElement('div');\n",
50 | " const label = document.createElement('label');\n",
51 | " label.textContent = `Downloading \"${filename}\": `;\n",
52 | " div.appendChild(label);\n",
53 | " const progress = document.createElement('progress');\n",
54 | " progress.max = size;\n",
55 | " div.appendChild(progress);\n",
56 | " document.body.appendChild(div);\n",
57 | "\n",
58 | " const buffers = [];\n",
59 | " let downloaded = 0;\n",
60 | "\n",
61 | " const channel = await google.colab.kernel.comms.open(id);\n",
62 | " // Send a message to notify the kernel that we're ready.\n",
63 | " channel.send({})\n",
64 | "\n",
65 | " for await (const message of channel.messages) {\n",
66 | " // Send a message to notify the kernel that we're ready.\n",
67 | " channel.send({})\n",
68 | " if (message.buffers) {\n",
69 | " for (const buffer of message.buffers) {\n",
70 | " buffers.push(buffer);\n",
71 | " downloaded += buffer.byteLength;\n",
72 | " progress.value = downloaded;\n",
73 | " }\n",
74 | " }\n",
75 | " }\n",
76 | " const blob = new Blob(buffers, {type: 'application/binary'});\n",
77 | " const a = document.createElement('a');\n",
78 | " a.href = window.URL.createObjectURL(blob);\n",
79 | " a.download = filename;\n",
80 | " div.appendChild(a);\n",
81 | " a.click();\n",
82 | " div.remove();\n",
83 | " }\n",
84 | " "
85 | ],
86 | "text/plain": [
87 | ""
88 | ]
89 | },
90 | "metadata": {
91 | "tags": []
92 | }
93 | },
94 | {
95 | "output_type": "display_data",
96 | "data": {
97 | "application/javascript": [
98 | "download(\"download_f15fb86d-600d-41bf-8211-279f0d41a4ac\", \"copy.txt\", 5)"
99 | ],
100 | "text/plain": [
101 | ""
102 | ]
103 | },
104 | "metadata": {
105 | "tags": []
106 | }
107 | }
108 | ]
109 | }
110 | ]
111 | }
--------------------------------------------------------------------------------
/script.py:
--------------------------------------------------------------------------------
1 | import time
2 | import os
3 | from selenium import webdriver
4 | from selenium.webdriver.common.action_chains import ActionChains
5 | from selenium.webdriver.common.keys import Keys
6 | from selenium.webdriver.support.wait import WebDriverWait
7 | from selenium.webdriver.firefox.options import Options
8 |
9 | fp = webdriver.FirefoxProfile('/home/asgaralipq/.mozilla/firefox/0zhbal6h.default-release')
10 | wd = webdriver.Firefox(fp)
11 | wd.get("https://colab.research.google.com/drive/1PIybc8oaszBcVeAPVQyAl-90jYDDMhWp")
12 | print(wd.title)
13 | print("Page loaded")
14 |
15 | wd.implicitly_wait(10)
16 | wd.find_element_by_id('runtime-menu-button').click()
17 | wd.find_element_by_id(':1w').click()
18 |
19 | print("Running colab...")
20 | print("Waiting for task to complete...")
21 |
22 | while not os.path.isfile('/home/asgaralipq/Work/Colab-Script/Downloads/copy.txt'):
23 | time.sleep(1)
24 |
25 | print("Task completed")
26 |
27 | time.sleep(5)
28 |
29 | print("Closing Driver...")
30 |
31 | wd.quit()
--------------------------------------------------------------------------------