{ "cells": [ { "cell_type": "markdown", "id": "866877cb-3b6a-4b44-b50d-e2af9e0472aa", "metadata": {}, "source": [ "

ICON Training - Hands-on Session

\n", "\n", "# Helper script: Batch job monitoring\n", "\n", "---\n", "\n", "This Jupyter notebook provides a method for continuously monitoring the status of your HPC jobs. The notebook cell below executes a loop that refreshes its output until the kernel is manually interrupted. It is important to choose an appropriate polling interval to avoid excessive load on the scheduler.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "561182ad-d5ad-4221-9cc2-5a4b9f39c247", "metadata": {}, "outputs": [], "source": [ "import asyncio\n", "import os\n", "import subprocess\n", "from IPython.display import display, clear_output\n", "\n", "async def poll_command(cmd, interval=10):\n", " while True:\n", " # Execute the command and capture output\n", " proc = await asyncio.create_subprocess_shell(\n", " cmd,\n", " stdout=asyncio.subprocess.PIPE,\n", " stderr=asyncio.subprocess.PIPE\n", " )\n", " stdout, stderr = await proc.communicate()\n", " output = stdout.decode() if stdout else ''\n", " error = stderr.decode() if stderr else ''\n", " \n", " clear_output(wait=True)\n", " print(f\"Command: {cmd}\\n\")\n", " print(output)\n", " if error:\n", " print(\"Error:\", error)\n", " \n", " await asyncio.sleep(interval) # Non-blocking sleep[3][5]\n", "\n", "# Get the current user from the environment\n", "user = os.environ.get('USER')\n", "cmd = f'squeue -u {user}; date'\n", "\n", "# Start the polling coroutine\n", "asyncio.ensure_future(poll_command(cmd, interval=10))" ] } ], "metadata": { "kernelspec": { "display_name": "1 Python 3 (based on the module python3/2023.01)", "language": "python", "name": "python3_2023_01" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" } }, "nbformat": 4, "nbformat_minor": 5 }