This script shows a notification when a job in any session runs for over thirty seconds.

It demonstrates creating a task for every session, monitoring for changes of the currently running command, posting notifications, and some basic asyncio usage.

This script requires that shell integration be installed in order to detect prompts and running commands.

#!/usr/bin/env python3.7

import asyncio
import iterm2

async def main(connection):
app = await iterm2.async_get_app(connection)

"""Show a user notification in session if this task is allowed to run
for 30 seconds."""
await asyncio.sleep(30)
await session.async_inject(str.encode(code))

async def monitor(session_id):
"""Wait for commands to start or stop in this session.

session = app.get_session_by_id(session_id)
if not session:
return
modes = [iterm2.PromptMonitor.Mode.PROMPT,
iterm2.PromptMonitor.Mode.COMMAND_START,
iterm2.PromptMonitor.Mode.COMMAND_END]
async with iterm2.PromptMonitor(
connection, session_id, modes=modes) as mon:
while True:
# This blocks until the status of the session changes. That
# happens when a new prompt appears, a command begins running,
# or a command finishes.
mode, info = await mon.async_get()
if mode == iterm2.PromptMonitor.Mode.COMMAND_START:
# A command has started running. Create a task that will
# alert if it runs for too long.
# Create a task running monitor for each session, including those created

Download